Android [intermediate tutorial] Chapter 6 XML Parser

Source: Internet
Author: User
Tags xml parser

Next, let's take a look at the SAX Parser. In fact, it's similar to the pull parser. In fact, it's enough to learn and master an XML parser to complete the project, of course, it is not a bad thing to learn more. What do you mean? Come on! Let's take the data of Tang Miao's migration as an example.

<? XML version = "1.0" encoding = "UTF-8"?> <Persons> <person id = "1"> <status> big brother </status> <Name> Sun Wukong </Name> <tool> golden hoop </tool> <number> killed 50 monsters </number> </person> <person id = "2"> <status> diaodi </status> <Name> pig </Name> <tool> nine-tooth dingtalk </tool> <number> killed 40 monsters </number> </person> <person id = "3"> <status> three younger siblings </status> <Name> Sha monk </Name> <tool> demon master pin </tool> <number> killed 30 monsters </number> </person> </persons>

 

The XML file is the same as that in the previous chapter. If you are not clear about it, refer to the previous chapter.

Android [intermediate tutorial] Chapter 5 pull Parser for XML Parsing

Here I mainly put the source code of saxhandler.

Import Java. util. arraylist; import Java. util. list; import Org. XML. sax. attributes; import Org. XML. sax. saxexception; import Org. XML. sax. helpers. defaulthandler; public class saxhandler extends defaulthandler {private list <person> persons; private person; private string tagname; // The function is to record the previous node name public list during parsing <person> getpersons () {return persons ;} /*** node Processing */@ overridepublic void characters (char [] CH, int start, int length) throws saxexception {string data = new string (CH, start, length ); if ("status ". equals (tagname) {person. setstatus (data);} If ("name ". equals (tagname) {person. setname (data);} If ("tool ". equals (tagname) {person. settool (data);} If ("number ". equals (tagname) {person. setnumber (data) ;}/ *** End Element */@ overridepublic void endelement (string Uri, string localname, string QNAME) throws saxexception {If ("person ". equals (localname) {persons. add (person); person = NULL;} tagname = NULL;}/*** document start */@ overridepublic void startdocument () throws saxexception {persons = new arraylist <person> ();}/*** element start */@ overridepublic void startelement (string Uri, string localname, string QNAME, attributes) throws saxexception {If ("person ". equals (localname) {person = new person (); person. setid (attributes. getvalue ("ID");} tagname = localname; // assign the name of the node being parsed to tagname }}

The following is the source code in the activity:

Import Java. io. file; import Java. io. fileinputstream; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import javax. XML. parsers. saxparser; import javax. XML. parsers. saxparserfactory; import android. app. activity; import android. OS. bundle; import android. OS. environment; import android. widget. listview; import android. widget. simpleadapter; public class pullactivity extends activity {private listview; private simpleadapter adapter; @ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. xml_handler); listview = (listview) findviewbyid (R. id. xml_list); try {// getadapter ();} catch (exception e) {e. printstacktrace ();} listview. setadapter (adapter);} // custom adaptation method private void getadapter () throws exception {list <Map <string, string> lists = new arraylist <Map <string, string >>(); // This part is the part of file sd_files = environment we used for testing. getexternalstoragedirectory (); string file_path = sd_files.getname () + file. separator + "persons. XML "; // fileinputstream input = new fileinputstream (new file (file_path); // pullhandler = new pullhandler (input); // list <person> Persons = pullhandler. getpersons (); fileinputstream FCM = new fileinputstream (new file (file_path); saxparser parser = saxparserfactory. newinstance (). newsaxparser (); saxhandler = new saxhandler (); parser. parse (FCM, saxhandler); List <person> Persons = saxhandler. getpersons (); // convert the data in persons to arraylist <Map <string, string>, because simpleadapter uses this type of data to adapt Map <string, string> map; for (person P: Persons) {map = new hashmap <string, string> (); map. put ("ID", p. GETID (); map. put ("status", p. getstatus (); map. put ("name", p. getname (); map. put ("tool", p. gettool (); map. put ("Number", p. getnumber (); lists. add (MAP) ;}// keystring [] From = {"ID", "status", "name", "tool", in hashmap <string, string> ", "Number"}; // idint [] to = {R. id. item_id, R. id. item_status, R. id. item_name, R. id. item_tool, R. id. item_number}; adapter = new simpleadapter (this, lists, R. layout. handler_list_item, from, );}}

Other parsing methods are the same as pull parsing. Here we only replace pull parsing with Sax parsing. In the source code, we replace the three lines of code in the middle with the code in the sax parsing ,:

Look, they are all the same. Next chapter will introduce the DOM parser. I hope you can study it well. Thank you.

 

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.