Android Sax Parsing notes

Source: Internet
Author: User

Books.xml:

<?XML version= "1.0" encoding= "Utf-8"?><Books>    < BookID= " a"Code= "a">        <name>Thinking in Java</name>        < Price>85.5</ Price>    </ Book>    < BookID= " the"Code= "B">        <name>Spring in Action</name>        < Price>39.0</ Price>    </ Book></Books>
View Code

Mycontenthandler.java:

 PackageCom.example.xmlparse;Importorg.xml.sax.Attributes;Importorg.xml.sax.SAXException;ImportOrg.xml.sax.helpers.DefaultHandler; Public classMycontenthandlerextendsDefaultHandler {PrivateString name; PrivateString Price; PrivateString TagName; @Override Public voidStartdocument ()throwssaxexception {//TODO auto-generated Method StubSystem.out.println ("..... startdocument ............ ...")); } @Override Public voidEnddocument ()throwssaxexception {//TODO auto-generated Method StubSystem.out.println ("..... enddocument ............ ...")); } @Override Public voidstartelement (String uri, String localname, String qName, Attributes Attributes)throwssaxexception {//TODO auto-generated Method StubSystem.out.println ("..... startelement ............ ...")); TagName=LocalName; if(Localname.equals ("book")) {            //get all the properties of a label             for(inti=0; I<attributes.getlength (); i++) {System.out.println (Attributes.getlocalname (i)+ " = " +Attributes.getvalue (i)); } }} @Override Public voidendElement (String uri, String localname, string qName)throwssaxexception {//TODO auto-generated Method StubSystem.out.println ("..... endElement ............ ...")); TagName= ""; if(Localname.equals ("book")) {             This. PrintOut (); }} @Override Public voidCharacters (Char[] ch,intStartintlength)throwssaxexception {//TODO auto-generated Method StubSystem.out.println ("..... characters ............ ..."));        System.out.println (TagName); if(Tagname.equals ("name") ) {Name=NewString (CH, start, length); }Else if(Tagname.equals ("Price") ) { price=NewString (CH, start, length); }    }        Private voidprintout () {System.out.println ("Name:" +name); System.out.println ("Price:" +Price ); }}
View Code

Execute code Result:

........ Startdocument..........................startelement..........................characters ... ..... books.............characters.............books.............startelement.............id= 12Code=a.............characters.............book.............characters.............book.............startelement ..... ..... ..... ..... ..... ..... ..... ..... ..... characters.............name.............endelement ..... ...... ..... Characters..........................characters..........................startelement ......... ........... Characters.............price.............endelement..........................characters ............ characters..........................endelement.............name:thinking in Javaprice:----- 85.5........ Characters..........................characters..........................startelement ... the ID of a..= 15Code=b.............characters.............book.............characters.............book.............startelement ..... ..... ..... ..... ..... ..... ..... ..... ..... characters.............name.............endelement ..... ...... ..... Characters..........................characters..........................startelement ......... ........... Characters.............price.............endelement..........................characters ............ characters..........................endelement.............name:spring in Actionprice:----- 39.0........ Characters..........................endelement..........................enddocument .............
View Code

Attention:

The line at the end of the XML message \ t at the beginning of the line will call characters

Android Sax parsing notes

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.