In the previous article, the use of the SAX parser to parse an XML file, the advantage of a SAX parser is that it consumes less memory. This article focuses on parsing XML files using the DOM parser. The advantages of DOM parsers may be intuitive to understand, and of course everyone may have different preferences for different analytic methods. But Dom parser has a big disadvantage, is to occupy more memory, in the Android XML parsing, or more recommend other parsing methods.
The following describes parsing XML using the DOM parser.
Here is the XML file we need to parse
<?xml version= "1.0" encoding= "UTF-8"? ><persons><person id= "All" ><name>liming</name> <age>30</age></person><person id= "><name>lixiangmei</name><age>25" </age></person></persons>
Our person entity class
public class Person {private Integer id;private String name;private short age;public person () {}public person (Integer ID, S Tring name, short age) {this.id = Id;this.name = Name;this.age = age;} Public Integer GetId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public short getage () {return age;} public void Setage {this.age = age;} @Overridepublic String toString () {return "person [age=" + Age + ", id=" + ID + ", name=" + name + "]";}}
Our parsing code, because the comments are more, so do not explain too much.
/** * Parsing XML using DOM * * @author Zhaokaiqiang * * @time June 3, 2014 */public class Domresovel {public static List<person > getpersons (InputStream inputstream) throws Exception {list<person> persons = new arraylist<person> (); /Get DOM parser Documentbuilder Documentbuilder = Documentbuilderfactory.newinstance (). Newdocumentbuilder ();// Gets the parsed document tree documents = Documentbuilder.parse (InputStream);//Get the root element node element root = Document.getdocumentelement ( );//Gets the root element below so the person node nodelist personlist = root.getelementsbytagname ("person");//traverse all the person nodes for (int i = 0; i < PE Rsonlist.getlength (); i++) {Person man = new Person ();//gets each man node Element personelement = (Element) personlist.item (i);// Sets the Person object property Person.setid (New Integer (Personelement.getattribute ("id")));//Gets the child node under person nodelist childlist = Personelement.getchildnodes ();//Traverse child node for (int j = 0; J < Childlist.getlength (); j + +) {//Determine if element node if (Childlist.item (j ). Getnodetype () = = Node.element_node) {ELEMENT chilelement = (Element) Childlist.item (j), if ("Name". Equals (Chilelement.getnodename ())) {Person.setname ( Chilelement.getfirstchild (). Getnodevalue ());} else if ("Age". Equals (Chilelement.getnodename ())) {Person.setage (New short (Chilelement.getfirstchild ()). Getnodevalue ()));}}} Persons.add (person);} return persons;}}
Here's our unit test method, unit testing the parser
public void Testdom () throws Exception {InputStream instream = GetClass (). getClassLoader (). getResourceAsStream (" Persons.xml "); list<person> persons = Domresovel.getpersons (instream); for (person person:persons) {log.i (TAG, person.tostring ( ));}}
The following is the result of parsing
In doubt, you can leave a message