XML file parsing on the fourth day of Android Development

Source: Internet
Author: User
Tags tagname

1. Sax Method

/*** Use the SAX Parser */public class saxparse {/*** SAX Parser */private saxparser parser; Public saxparse () {try {saxparserfactory F = saxparserfactory. newinstance (); parser = f. newsaxparser ();} catch (parserconfigurationexception e) {e. printstacktrace ();} catch (exception e) {e. printstacktrace () ;}} public list <person> doparse (inputstream is) {try {xmlhandler H = new xmlhandler (); parser. parse (is, H); Return H. getpersons ();} catch (exception e) {e. printstacktrace ();} return NULL;}/*** processor */class xmlhandler extends defaulthandler {list <person> Persons = NULL; person = NULL; // The current element name is Private string currelename;/*** this method is triggered by a text node */Public void characters (char [] CH, int start, int length) throws saxexception {string STR = new string (CH, start, length); // name if ("name ". equals (currelename) {person. name = STR;} else if ("age ". equals (currelename) {person. age = integer. parseint (STR) ;}} public void enddocument () throws saxexception {}/ *** element end */Public void endelement (string Uri, string localname, string QNAME) throws saxexception {If ("person ". equals (localname) {persons. add (person);} // set the current element to else if ("name ". equals (currelename) | ("age ". equals (currelename) {This. currelename = "" ;}}/*** document start event */Public void startdocument () throws saxexception {persons = new arraylist <person> ();} /*** element start event * localname: local place name * URI: namespace * QNAME: qualified name, prefix + local place name */Public void startelement (string Uri, string localname, string QNAME, attributes) throws saxexception {// instantiate the person object if ("person ". equals (localname) {person = new person (); person. id = integer. parseint (attributes. getvalue (0);} // name element else if ("name ". equals (localname) {This. currelename = "name";} // name element else if ("age ". equals (localname) {This. currelename = "Age" ;}} public list <person> getpersons () {return persons ;}}}

2. Dom Mode

/*** Dom parsing */public class domparse {// Private documentbuilder builder; Public domparse () {try {documentbuilderfactory F = documentbuilderfactory. newinstance (); this. builder = f. newdocumentbuilder ();} catch (exception e) {e. printstacktrace () ;}} public list <person> doparse (inputstream is) {list <person> Persons = new arraylist <person> (); person = NULL; try {document DOC = builder. parse (is); nodelist list = Doc. getelementsbytagname ("person"); element ele = NULL; For (INT I = 0; I <list. getlength (); I ++) {ele = (element) list. item (I); person = new person (); person. id = integer. parseint (Ele. getattribute ("ID"); person. name = getsubelementtextcontent (Ele, "name"); person. age = integer. parseint (getsubelementtextcontent (Ele, "Age"); persons. add (person) ;}} catch (exception e) {e. printstacktrace ();} return persons;}/*** get the text content in the middle of the specified resource */private string getsubelementtextcontent (element ele, string tagname) {nodelist list = ELE. getelementsbytagname (tagname); Element E = (element) list. item (0); // obtain the intermediate text node Return e. gettextcontent ();}}

3. Pull Mode

/*** Pull resolution, pull mode, you can manually control whether the next event is triggered. */public class pullparse {public list <person> doparse (inputstream is) {list <person> Persons = NULL; person = NULL; try {xmlpullparser parser = xml. newpullparser (); // sets the parsing data source parser. setinput (is, "UTF-8"); // retrieves the event type int eventtype = parser. geteventtype (); string elename = NULL; while (eventtype! = Xmlpullparser. end_document) {Switch (eventtype) {// The document starts case xmlpullparser. start_document: Persons = new arraylist <person> (); break; // element start case xmlpullparser. start_tag: elename = parser. getname (); If ("person ". equals (elename) {person = new person (); person. id = integer. parseint (parser. getattributevalue (0);} else if ("name ". equals (elename) {person. name = parser. nexttext ();} else if ("age ". equals (elename) {person. age = integer. parseint (parser. nexttext ();} break; // mark the end case xmlpullparser. end_tag: elename = parser. getname (); If ("person ". equals (elename) {persons. add (person) ;}break ;}// manually activate the trigger eventtype = Parser for the next event. next () ;}} catch (exception e) {e. printstacktrace ();} return persons ;}}

Mobile development QQ group: 59516399

Csdn download link: http://download.csdn.net/detail/wenwei19861106/4975192

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.