An example of using Sax to parse XML in Android

Source: Internet
Author: User

The config. xml file is as follows:

<Config> <br/> <string-array name = "system_app_ti60"> <br/> <item> browser.apk </item> <br/> <item> phone.apk </item> <br/> <item> contact.apk </item> <br/> <item> settings.apk </item> <br/> <item> gmail.apk </item> <br/> </string-array> <br/> </config>

 

The following are the main classes used:

Import org. xml. Sax. attributes;
Import org. xml. Sax. saxexception;
Import org. xml. Sax. helpers. defaulthandler;
Import javax. xml. parsers. saxparserfactory;
Import javax. xml. parsers. saxparser;
Import javax. xml. parsers. parserconfigurationexception;

 

Xmlparser class: used to process read data, such as tags and text.

Private class xmlparser extends defaulthandler <br/>{< br/> private arraylist <string> array = new arraylist <string> (); <br/> string mname = NULL; <br/> private Boolean mfound = false; </P> <p> Public arraylist <string> getarraystring () {<br/> return array; <br/>}< br/> // callback when the start tag information is read <br/> @ override <br/> Public void startelement (string Uri, string localname, string QNAME, attributes) THR Ows saxexception {<br/> If (localname. equals ("string-array") {<br/> mname = attributes. getvalue (0); <br/> If (mname. equals ("system_app_ti60") {<br/> mfound = true; <br/>}</P> <p >}< br/> // callback when reading text information <br/> @ override <br/> public void characters (char ch [], int start, int length) throws saxexception {<br/> If (mfound) {<br/> string value = new string (CH, start, length ); <br/> If (! Value. endswith ("APK") <br/> return; <br/> array. add (value ); <br/>}< br/> // callback when the end tag information is read <br/> @ override <br/> Public void endelement (string Uri, string localname, string QNAME) throws saxexception {<br/> If (localname. equals ("string-array") {<br/> If (mfound) {<br/> mfound = false; <br/>}</P> <p >}< br/>}

 

Call test:

Try {<br/> saxparserfactory factory = saxparserfactory. newinstance (); <br/> saxparser parser = factory. newsaxparser (); <br/> xmlparser handler = new xmlparser (); <br/> parser. parse (new file ("/system_extend/system_extend_config.xml"), Handler); <br/> applist = handler. getarraystring (); </P> <p >}< br/> catch (saxexception e) {<br/> log. I ("hxd", "saxexception"); <br/> return; <br/>}< br/> catch (parserconfigurationexception e) {<br/> log. I ("hxd", "parserconfigurationexception"); <br/> return; <br/>}< br/> catch (ioexception e) {<br/> log. I ("hxd", "ioexception"); <br/> return; <br/>}< br/>}

 

 

 

 

 

 

 

 

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.