Android learning notes-XML parsing (SAX)

Source: Internet
Author: User

Sax is a parser that consumes less memory and is fast to parse. It uses event initiation and does not need to parse a complete document, instead, check whether a part of the document conforms to the XML syntax in order of content. If yes, the corresponding event is triggered. The so-called event is a callback method. These methods are defined in contenthandler, the main method is as follows:

Startdocument: trigger this event when a document is encountered. Call this method to do some preprocessing work. startelement: (string namespaceuri, string localname, string QNAME, attributes ATTS) this method is triggered when a tag is started. Endelement (string Uri, string localname, string name): this event is triggered when an end tag is encountered, and this method can be called to handle the problem. Charachers (char [] CH, int start, int length): This method is triggered when XML content is encountered. The content can be accepted using new string (CH, start, length. Mycontenthandler. Java

Package Mars. XML; </P> <p> Import Org. XML. sax. attributes; <br/> Import Org. XML. sax. saxexception; <br/> Import Org. XML. sax. helpers. defaulthandler; </P> <p> public class mycontenthandler extends defaulthandler {<br/> string hisname, address, money, sex, status; <br/> string tagname; </P> <p> Public void startdocument () throws saxexception {<br/> system. out. println ("''' begin '''"); <br/>}</P> <p> Public void enddocument () throws saxexception {<br/> system. out. println ("''' end ''''''''"); <br/>}</P> <p> Public void startelement (string namespaceuri, string localname, <br/> string QNAME, attributes ATTR) throws saxexception {<br/> tagname = localname; <br/> If (localname. equals ("worker") {<br/> // obtain all attributes of a tag <br/> for (INT I = 0; I <ATTR. getlength (); I ++) {<br/> system. out. println (ATTR. getlocalname (I) + "=" + ATTR. getvalue (I); <br/>}</P> <p> Public void endelement (string namespaceuri, string localname, string QNAME) <br/> throws saxexception {<br/> // After the workr tag is parsed, all the data obtained is printed. <br/> tagname = ""; <br/> If (localname. equals ("worker") {<br/> This. printout (); <br/>}< br/> Public void characters (char [] CH, int start, int length) <br/> throws saxexception {<br/> If (tagname. equals ("name") <br/> hisname = new string (CH, start, length); <br/> else if (tagname. equals ("sex") <br/> sex = new string (CH, start, length); <br/> else if (tagname. equals ("status") <br/> Status = new string (CH, start, length); <br/> else if (tagname. equals ("Address") <br/> address = new string (CH, start, length); <br/> else if (tagname. equals ("money") <br/> money = new string (CH, start, length); <br/>}</P> <p> private void printout () {<br/> system. out. print ("name:"); <br/> system. out. println (hisname); <br/> system. out. print ("Sex:"); <br/> system. out. println (sex); <br/> system. out. print ("status:"); <br/> system. out. println (Status); <br/> system. out. print ("Address:"); <br/> system. out. println (Address); <br/> system. out. print ("Money:"); <br/> system. out. println (money); <br/> system. out. println (); <br/>}</P> <p >}< br/>

Xmlactitity. Java Package Mars. XML; </P> <p> Import Java. io. stringreader; </P> <p> Import javax. XML. parsers. saxparserfactory; </P> <p> Import Mars. utils. httpdownloader; </P> <p> Import Org. XML. sax. inputsource; <br/> Import Org. XML. sax. xmlreader; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; </P> <p> public class xmlactitity extends activity {<br/>/** called when the activity is first created. */<br/> private button parsebutton; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> parsebutton = (button) findviewbyid (R. id. parsebutton); <br/> parsebutton. setonclicklistener (New parsebuttonlistener (); <br/>}</P> <p> class parsebuttonlistener implements onclicklistener {</P> <p> Public void onclick (view V) {<br/> httpdownloader Hd = new httpdownloader (); <br/> string resultstr = HD. download ("http: // 192.168.1.107: 8081/voa1500/test. XML "); <br/> system. out. println (resultstr); <br/> try {<br/> // create a saxparserfactory <br/> saxparserfactory factory = saxparserfactory. newinstance (); <br/> xmlreader reader = factory. newsaxparser (). getxmlreader (); <br/> // set the content processor for xmlreader <br/> reader. setcontenthandler (New mycontenthandler (); <br/> // start File Parsing <br/> reader. parse (New inputsource (New stringreader (resultstr); <br/>}< br/> catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p >}< br/>}

Related Article

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.