Talking about parsing XML document with Java (II.)

Source: Internet
Author: User
Tags stub

The previous article summarizes the way DOM parses XML documents, and this article begins with a summary of how SAX parses XML and its pros and cons!

SAX (Simple API for XML) refers to an interface, or a package.

First we should know the difference between Sax parsing and Dom parsing:

Dom is a tree structure parsing, easy to understand and develop, it can freely access the location of the node where the file, easy to modify, delete and query. However, when the DOM document is too large, it resolves slowly.

Sax parsing is the event-driven, sequential reading of the file's node, and can only read the contents of the document, the content of the document can not be repaired, the size of the document is not too restrictive, but the large complexity of opening,

Here are a few key steps for a SAX parsing XML document:

(1), creating an XML parsing processor, which is an instance of Saxparsefactory,

SAXParserFactory factory = Saxparserfactory.newinstance ();

(2), create SAX parser through factory

SAXParser parser = Factory.newsaxparser ();

(3), through the Saxparse instance, create the XML parsing processor. The document is parsed,

Parser.parse (Uri,handler) method has two parameter URI, handler object, URI is the path of our document,

Next we need to create a handler object, we create a MyHandler class, the MyHandler class needs to inherit the DefaultHandler class,

MyHandler handler = new MyHandler ();

Here is the example code for our MyHandler.

1  PackageCom.imooc.handler;2 3 Importorg.xml.sax.Attributes;4 Importorg.xml.sax.SAXException;5 ImportOrg.xml.sax.helpers.DefaultHandler;6 7 /*8 * Parse XML processor9  *Ten  * */ One  Public classMyHandlerextendsdefaulthandler{ AString value =NULL; -     /** - * Used to identify the start of parsing the      */ - @Override -      Public voidStartdocument ()throwssaxexception { -         //TODO auto-generated Method Stub +         Super. Startdocument (); -System.out.println ("Sax parsing begins"); +     } A      at     /** - * Used to identify the end of parsing -      */ - @Override -      Public voidEnddocument ()throwssaxexception { -         //TODO auto-generated Method Stub in         Super. Enddocument (); -System.out.println ("Sax parsing End"); to     } +      -     /** the * Parsing XML elements *      */ $ @OverridePanax Notoginseng      Public voidstartelement (String uri, String localname, String qName, -Attributes Attributes)throwssaxexception { the         //TODO auto-generated Method Stub +         Super. Startelement (URI, LocalName, qName, attributes); A         if(Qname.equals ("book")) { the             intnum =attributes.getlength (); +              for(inti = 0; i < num; i++){ -System.out.print ("The book Element's" + (i + 1) + "attribute name is:" $+Attributes.getqname (i)); $SYSTEM.OUT.PRINTLN ("---property value is:" +Attributes.getvalue (i)); -              -             } the}Else if(!qname.equals ("name") &&!qname.equals ("bookstore")) { -             WuyiSystem.out.print ("Node name is:" + qName + "---"); the         } -     } Wu  - @Override About      Public voidendElement (String uri, String localname, string qName) $             throwssaxexception { -         //TODO auto-generated Method Stub -         Super. EndElement (URI, LocalName, qName); -          A     } +  the @Override -      Public voidCharacters (Char[] ch,intStartintlength) $             throwssaxexception { the         //TODO auto-generated Method Stub the         Super. Characters (ch, start, length); theValue =NewString (CH, start, length); the         if(!value.trim (). Equals ("")) { -System.out.println ("Node value is:" +value); in         } the     } the  About      the      the}

(4), test the main program entrance

      

1  Public Static voidMain (string[] args) {2         3SAXParserFactory factory =saxparserfactory.newinstance ();4         5         Try {6SAXParser parser =Factory.newsaxparser (); 7         8MyHandler handler =NewMyHandler ();9Parser.parse ("books.xml", handler);Ten          One}Catch(parserconfigurationexception e) { A             //TODO auto-generated Catch block - e.printstacktrace (); -}Catch(saxexception e) { the             //TODO auto-generated Catch block - e.printstacktrace (); -}Catch(IOException e) { -             //TODO auto-generated Catch block + e.printstacktrace (); -         } +}

books.xml file,

      

1<?xml version= "1.0" encoding= "UTF-8"?>2<bookstore>3<book id= "1" >4<name> old man with sea </name>5<author> Hemingway </author>6<year>1955</year>7<price>45</price>8</book>9<book id= "2" >Ten<name> Book Swords </name> One<year>1959</year> A<price>24</price> -<language>chinese</language> -</book> the</bookstore>

The result of the output is:

    

Sax parsing begins the 1th property name of the book element is: ID---The value of the property is: 1 node value is: The old Man and the Sea festival name is: Author--- node value is: Ernest Hemingway Festival name is:Year--- The node value is: The 1955 node name is: Price---The value of the nodes is:the 1th property name of the second book elementis: id---attribute value is: 2 node value is: The Sword and the name is:year--- The node value is: the 1959 node name is: Price---The value of the nodes is: the language--- node value is: Chinesesax parsing end

This allows us to complete the most basic Sax parsing XML document.

Summarize:

    Advantages : The memory requirements are relatively low, the part of the data in the document to parse the development speed, and the ability to expand.

    cons : XML parsing with sax requires sequential execution, making it difficult to access different data in the same document. In addition, the process of parsing and coding based on this approach is also relatively complex

        

Talking about parsing XML document with Java (II.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.