1. Introduction to DOM parsing DOM parsing stores the contents of an XML file in memory in the form of an object tree. Dom parsing takes the form of object tree storage, making it easy to traverse, easy to know the situation of the upper and lower nodes, which is the biggest difference between DOM parsing and sax parsing. Sax parsing can only know the situation of its own node, and cannot learn the information of other nodes.
2. Dom parsing common method (1) document.getdocumentelement (): Returns the root element of the document
(2) node.getprevioussibling (), node.getnextsibling (): Returns the previous or next sibling element
(3) Node.getfirstchild (), Node.getlastchild (): Returns the first child node or the last node of a given node
(4) Node.getattribute (attrname): Gets the object of the specified property
3. Dom parsing ideas (1) Needless to say, the first step is to create an entity object that stores the XML file read information
(2) Create a Documentbuilderfactory object that gets the Documentbuilder object based on it
(3) The Documentbuilder instance calls the parse method, and the passed argument is the read file stream object
(4) Traverse the XML file according to the node name, attribute value and other information, store it.
4. Example 4.1 New entity:
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;} Omit setter () and Getter () method}
4.2 Parsing classes:
public static list<person> getpersons (InputStream instream) throws exception{list<person> persons = new Arraylist<person> ();D ocumentbuilderfactory factory = documentbuilderfactory.newinstance ();D Ocumentbuilder Builder = Factory.newdocumentbuilder ();D ocument Document = Builder.parse (instream); Element root = Document.getdocumentelement (); NodeList personnodes = root.getelementsbytagname ("person"), for (int i=0; i < personnodes.getlength (); i++) {Element Personelement = (Element) personnodes.item (i); int id = new Integer (Personelement.getattribute ("id")); person person = new person ();p Erson.setid (ID); NodeList childNodes = Personelement.getchildnodes (); for (int y=0; y < Childnodes.getlength (); y++) {if ( Childnodes.item (y). Getnodetype () ==node.element_node) {if ("name". Equals (Childnodes.item (y). Getnodename ())) { String name = Childnodes.item (y). Getfirstchild (). Getnodevalue ();p erson.setname (name); else if ("Age". Equals (Childnodes.item (y). Getnodename ())) {String age = Childnodes.item(y). Getfirstchild (). Getnodevalue ();p erson.setage (new short);}} Persons.add (person);} Instream.close (); return persons;}
5. Comparing sax Parsing (1) the event driver for Sax is the callback method, which requires a callback method.
(2) Dom parsing is a tree structure, nodes and nodes have this hierarchical relationship, we simply give a diagram as follows:
We look at the contents of the XML file and parse out the tree structure diagram:
Dom parsing methods in Android