Android [intermediate tutorial] Chapter 7 Dom Parser for XML Parsing

Source: Internet
Author: User

Next, we will introduce Dom parsing in this chapter, Because Dom is a lot of parser used in J2EE. The parsing method here is the same as that of J2EE, the specific style is the same as the style in the following article.

<? 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>
For other data or styles, see the following Tutorial: Android [intermediate tutorial] Chapter 5 pull Parser for XML Parsing

Let's look at the code at the resolution:

Import Java. io. inputstream; import Java. util. arraylist; import Java. util. list; import javax. XML. parsers. documentbuilder; import javax. XML. parsers. documentbuilderfactory; import Org. w3C. dom. document; import Org. w3C. dom. element; import Org. w3C. dom. node; import Org. w3C. dom. nodelist; public class domhandler {private inputstream input; private list <person> persons; private person; Public domhandler () {} public domhandler (inputstream input) {This. input = input;} public void setinput (inputstream input) {This. input = input;} public list <person> getpersons () {persons = new arraylist <person> (); try {// use the DOM factory method to create the DOM parser documentbuilder builder = documentbuilderfactory. newinstance (). newdocumentbuilder (); document = builder. parse (input); element = document. getdocumentelement (); // get the node list of the node <person> nodelist personnodes = element. getelementsbytagname ("person"); // node length int length = personnodes. getlength (); For (INT I = 0; I <length; I ++) {// get the <person> node element personelement = (element) personnodes. item (I); person = new person (); // gets the ID attribute value "person" in <person id = "1">. setid (personelement. getattribute ("ID"); // continue down to obtain the subnode list, such as nodelist childnodes = personelement. getchildnodes (); int Len = childnodes. getlength (); For (Int J = 0; j <Len; j ++) {// if the child node is an element node if (childnodes. item (j ). getnodetype () = node. element_node) {// obtain the node name string nodename = childnodes. item (j ). getnodename (); // get the node value string nodevalue = childnodes. item (j ). getfirstchild (). getnodevalue (); If ("status ". equals (nodename) {person. setstatus (nodevalue);} If ("name ". equals (nodename) {person. setname (nodevalue);} If ("tool ". equals (nodename) {person. settool (nodevalue);} If ("number ". equals (nodename) {person. setnumber (nodevalue) ;}}// end for J persons. add (person);} // end for ireturn persons;} catch (exception e) {e. printstacktrace ();} return NULL ;}}

Then the code of the activity is:

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 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 "; // pull parsing document // fileinputstream input = new fileinputstream (new file (file_path); // pullhandler = new pullhandler (input ); // list <person> Persons = pullhandler. getpersons (); // sax parsing document // 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 (); // Dom parsing documentation fileinputstream FCM = new fileinputstream (new file (file_path); domhandler = new domhandler (FCM); List <person> Persons = domhandler. 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, );}}

In fact, Dom is rarely used in Android, mainly considering performance issues. After all, mobile phones are no better than servers or computers.

In the next chapter, we will introduce the JSON parsing method. I hope you will learn it carefully.

Haha, the long holiday in October will soon end, and I have to work hard and learn again. Thank you.
 

 

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.