Java parsing XML's sax way

Source: Internet
Author: User

Java Parsing XML sax way sax parsing XML steps

Get the SAXParserFactory instance by Saxparsefactory's static newinstance () method factory
Returns an SAXParser instance by using the Newsaxparser () method of the SAXParserFactory instance parser
Create a class that inherits Defaulthandle, overrides the method for business processing, and creates an instance of this class handle

Methods for overriding the Defaulthandle class

The Startelement method is used to iterate through the start tag of an XML file;

The EndElement method is used to traverse the end tag of an XML file;

The Startdocument method is used to identify the start of parsing;

The Enddocument method is used to identify the parsing end.

The characters method is used to get the text

Where: The name of the element to which the parameter QName is traversed

And there will also be whitespace and line wrapping issues

Directly below the code
<?XML version= "1.0" encoding= "UTF-8"?><Bookstore>    < BookID= "1">        <name>Java Programming Ideas</name>        <Anthor>****</Anthor>        < Year>2000</ Year>    </ Book>    < BookID= "2">        <name>Crazy Java Series</name>        <Anthor>Li Gang</Anthor>        < Price>89</ Price>    </ Book></Bookstore>
books.xmlSax.java
 PackagePers.zww.xml.handler;Importjavax.xml.stream.events.StartElement;Importorg.xml.sax.Attributes;Importorg.xml.sax.SAXException;ImportOrg.xml.sax.helpers.DefaultHandler; Public classSaxparserhandlerextendsdefaulthandler{intBookindex=0; /** Parsing XML elements*/@Override Public voidstartelement (String uri, String localname, String qName, Attributes Attributes)throwssaxexception {Super. Startelement (URI, LocalName, qName, attributes); //begin parsing the properties of the book element                if(Qname.equals ("book") ) {Bookindex++; System.out.println ("Start traversing the book" +bookindex+ "); //            //property name under the known book element, which gets the property value based on the property name//String value=attributes.getvalue ("id");//System.out.println ("book's Attribute Value:" +value); //name and number of attributes under unknown book element            intnum=attributes.getlength ();  for(inti = 0; i < num; i++) {System.out.print ("Book element's" + (i+1) + "attribute name:" +Attributes.getqname (i)); System.out.println ("&& attribute value:" +Attributes.getvalue (i)); }        }Else if(!qname.equals ("book") &&!qname.equals ("bookstore")) {System.out.print ("Node Name:" +qName); }Else{}} @Override Public voidCharacters (Char[] ch,intStartintlength)throwssaxexception {//TODO auto-generated Method Stub        Super. Characters (ch, start, length); String Val=NewString (CH, start, length); if(!val.trim (). Equals ("") {System.out.println ("&& node Value:" +val); }    }        /** Used to traverse the end tag of an XML file*/@Override Public voidendElement (String uri, String localname, string qName)throwssaxexception {Super. EndElement (URI, LocalName, QName); //whether for a book has traversed the end        if(Qname.equals ("book") {System.out.println ("======================="); }    }        /** Used to mark the start of parsing*/@Override Public voidStartdocument ()throwssaxexception {//TODO auto-generated Method Stub        Super. Startdocument (); //start of the first lineSystem.out.println ("Sax parsing begins"); }        /** Used to mark the end of parsing*/@Override Public voidEnddocument ()throwssaxexception {//TODO auto-generated Method Stub        Super. Enddocument (); //end of last lineSystem.out.println ("Sax parsing End"); }}
Saxparserhandler.javaSax vs. Dom

Dom parsing principle: The first XML file loaded into memory, in one-by-one parsing;

Sax parsing principle: through the handler class you create, to analyze each node encountered, (node analysis is to start from the outermost layer to the innermost one).

Resources

Mu class net Jessicajiang teacher Video Tutorial Links: http://www.imooc.com/video/3789

Java parsing XML's sax way

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.