Sax parsing xml file

Source: Internet
Author: User

<?XML version= "1.0" encoding= "UTF-8" standalone= "no"?><Bookshelf>    < Book>        <nameBookName= "Thinkinjava">Think in Java</name>        <author>Bruce Eckel</author>        < Price>95.00</ Price>    </ Book>    < Book>        <name>Effective Java</name>        <author>Joshua Bloch</author>        < Price>52.00</ Price>    </ Book></Bookshelf>
Importorg.xml.sax.*;ImportOrg.xml.sax.helpers.DefaultHandler;Importjavax.xml.parsers.ParserConfigurationException;ImportJavax.xml.parsers.SAXParser;Importjavax.xml.parsers.SAXParserFactory;Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.List;Importjava.util.Objects; Public classSaxdemo { Public Static voidMain (string[] args)throwsparserconfigurationexception, Saxexception, IOException {saxparserfactory factory=saxparserfactory.newinstance (); SAXParser Parser=Factory.newsaxparser (); XMLReader Reader=Parser.getxmlreader (); /*Reader.setcontenthandler (New Listhandle ());*/        /*Reader.setcontenthandler (New Tagvaluehandle ());*/Beanlisthandle Beanlisthandle=NewBeanlisthandle ();        Reader.setcontenthandler (Beanlisthandle); Reader.parse ("Day02/src/book.xml"); List<Book> list =beanlisthandle.getlist ();    SYSTEM.OUT.PRINTLN (list); }}//traversal read XML fileclassListhandleImplementsContentHandler {@Override Public voidsetdocumentlocator (Locator Locator) {} @Override Public voidStartdocument ()throwssaxexception {} @Override Public voidEnddocument ()throwssaxexception {} @Override Public voidstartPrefixMapping (string prefix, string uri)throwssaxexception {} @Override Public voidendprefixmapping (String prefix)throwssaxexception {} @Override Public voidStartelement (String uri, String localname, String qName, Attributes atts)throwssaxexception {System.out.println ("<" + QName + ">"); //Label Properties         for(inti = 0; Atts! =NULL&& i < atts.getlength (); i++) {String name=Atts.getqname (i); String value=Atts.getvalue (i); SYSTEM.OUT.PRINTLN (Name+ " - " +value); }} @Override Public voidEndElement (String uri, String localname, String qName)throwssaxexception {System.out.println ("</" + QName + ">"); } @Override Public voidCharacters (Char[] ch,intStartintLengththrowssaxexception {System.out.println (NewString (CH, start, length)); } @Override Public voidIgnorablewhitespace (Char[] ch,intStartintLengththrowssaxexception {} @Override Public voidProcessingInstruction (string target, string data)throwssaxexception {} @Override Public voidSkippedentity (String name)throwssaxexception {}}//gets the value of the specified labelclassTagvaluehandleextendsDefaultHandler {PrivateString Currenttag;//the currently resolved label    PrivateInteger currentindex = 0;//Current Parse Order@Override Public voidStartelement (String uri, String localname, String qName, Attributes Attributes)throwssaxexception {Currenttag=QName; if(Currenttag.equals ("Author") ) {Currentindex++; }} @Override Public voidCharacters (Char[] ch,intStartintLengththrowssaxexception {Integer index= 1;//the value of the first tag is required        if(Currenttag! =NULL&& currenttag.equals ("author") &&objects.equals (currentindex, index)) {System.out.println (NewString (CH, start, length)); }} @Override Public voidEndElement (String uri, String localname, String qName)throwssaxexception {Currenttag=NULL; }}//XML objects encapsulated as BeansclassBeanlisthandleextendsDefaultHandler {Privatelist<book> list =NewArraylist<>(); PrivateString Currenttag; PrivateBook Book ; @Override Public voidStartelement (String uri, String localname, String qName, Attributes Attributes)throwssaxexception {Currenttag=QName; if(Currenttag.equals ("book") ) { Book=NewBook (); }} @Override Public voidCharacters (Char[] ch,intStartintLengththrowssaxexception {if(Currenttag! =NULL&& currenttag.equals ("name") {book.setname (NewString (CH, start, length)); }        if(Currenttag! =NULL&& currenttag.equals ("Author") {Book.setauthor (NewString (CH, start, length)); }        if(Currenttag! =NULL&& currenttag.equals ("Price") {Book.setprice (NewString (CH, start, length)); }} @Override Public voidEndElement (String uri, String localname, String qName)throwssaxexception {Currenttag=NULL; if(Qname.equals ("book") {list.add (book); Book=NULL; }    }     PublicList<book>getList () {returnlist; }}

Sax parsing xml file

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.