Android Study Notes 42: XML file parsing (DOM Mode)

Source: Internet
Author: User

In the previous blog titled Android study note 41: XML file parsing (using the sax method), we learned how to use the SAX Parser to parse XML files, to obtain the useful information we want.

This blog article describes how to use the DOM parser to parse XML files.

 

1. Object Type in the DOM tree

When you use Dom to parse an XML document, the entire XML document is converted into a DOM tree, And the DOM parser converts the XML document node to each node in the DOM tree.

The DOM tree not only describes the structural features of XML documents, but also has the characteristics of object models. The process of converting XML documents into a DOM tree is the process of visualizing the document model.

All nodes in the DOM tree are node objects, as shown in subinterface 1 contained in the node interface.

Figure 1 subinterfaces contained in the node interface

After learning about the types of objects in the DOM model, you canProgramUse the attributes and methods of these objects to access the data they contain.

 

2. Dom parser

The following three steps can be used to create a DOM parser and visualize the XML document model.

(1) create a DOM parser factory object by calling the newinstance () method of the documentbuilderfactory class.

(2) create a DOM parser object by calling the newdocumentbuilder () method of the DOM parser factory object.

(3) Call the parse () method of the DOM parser object to visualize the document model and parse the XML document into a document object.

Use the preceding three stepsCodeThe implementation is as follows:

  1   2   //   Create a DOM parser factory   3  documentbuilderfactory factory =  documentbuilderfactory. newinstance ();   4   5   //   Create a DOM parser   6  documentbuilder =  factory. newdocumentbuilder ();   7   8   //   parse the XML document and obtain the document object corresponding to the XML document   9  document = documentbuilder. parse (inputstream); 

The above code parses the XML document into a document object in the form of an inputstream input stream. In fact, the documentbuilder abstract class contains multiple overloaded parse () methods, which can be used to parse different forms of XML documents into document objects, as shown below.

(1) Document parse (file F );

(2) Document parse (inputsourse is );

(3) Document parse (inputstream is );

(4) Document parse (inputstream is, string systemid );

(5) Document parse (string URI );

 

3. Use Dom to parse XML documents

3.1 get XML elements

Through the above method, we have obtained the Document Object corresponding to the XML document. The document contains the following methods to access nodes in the XML document.

(1) element getdocumentelement ();

(2) element getelementbyid (string elementid );

(3) nodelist getelementbytagname (string tagname );

The getdocumentelement () method is used to obtain the root node of the XML document. The getelementbyid () method is used to obtain the XML Element Based on the ID of the XML element. getelementbytagname () the method is used to obtain the XML Element Based on the Tag Name of the XML element.

3.2 get XML Element Content

Element is the most common subinterface of node, representing an XML element. The element interface provides the following methods to obtain the content of XML elements.

(1) string getattribute (string name );

(2) ATTR getattributenode (string name );

(3) nodelist getelementbytagname (string name );

(4) string gettagname ();

(5) Boolean hasattribute (string name );

The getattribute () method is used to obtain the specified attribute value based on the attribute name. The getattributenode () method is used to obtain the specified attribute node based on the attribute name. getelementbytagname () the gettagname () method is used to obtain the nodelist composed of all child elements contained in the element based on the tag name. The gettagname () method is used to obtain the Tag Name of the element. hasattribute () the method is used to determine whether the element contains a specified attribute.

3.3 get node data

In the DOM tree, all nodes are node objects, and each node object may include attributes such as nodename, nodevalue, and attributes. Note that different node nodes have different support for these three attributes.

The node interface provides the following common methods to obtain node data.

(1) nodelist getchildnodes (); // obtain the nodelist composed of all the subnodes contained by the node

(2) node getfirstchild (); // obtain the first subnode contained in the node

(3) node getlastchild (); // obtain the last child node contained in the node

(4) string getlocalname (); // obtain the label name of the node (local part)

(5) string getnodename (); // obtain the node name of the node.

(6) string getnodevalue (); // obtain the node value of the node.

(7) Document getownerdocument (); // obtain the Document Object of the node

(8) node getparentnode (); // obtain the parent node of the node

(9) string gettextcontent (); // obtain the text content of the node and Its subnodes

(10) string getattributenode (string name); // obtain the specified attribute value based on the attribute name

 

4. Instance

After learning about the methods used to obtain XML elements and the content of XML elements, you can call these methods to parse XML documents.

Here, we still take the "person. xml" document mentioned in the previous blog as an example. The content of "person. xml" is as follows:

 1  <?  XML version = "1.0" encoding = "UTF-8"  ?>  2  <  Persons  >  3  <  Person  ID  = "1"  >  4  <  Name  > Jack </  Name  > 5  <  Age  > 24 </  Age  >  6  </  Person  >  7  <  Person  ID  = "2" >  8  <  Name  > Tom </  Name  >  9  <  Age  > 25 </  Age  >  10  </  Person  >  11  <  Person  ID  = "3"  >  12  <  Name  > Bob </  Name  > 13  <  Age  > 22 </  Age  >  14  </  Person  >  15  </  Persons  > 

Based on the above XML document content, we can create a person class to store the content information in the <person> </person> label. What we need to do is to parse the ID, name, and age information contained in the <person> </person> label and store it in the person object.

The following code provides an implementation scheme:

 1       /*  2   * Function: Use the DOM parser to parse XML documents.  3   * Param: inputstream transmits the XML document as an input stream  4   * Retuen: List <person> person Object List  5   * Author: blog Park-still indifferent 6        */  7       Public   Static List <person> readxml (inputstream) Throws  Exception {  8           9 Documentbuilderfactory factory = documentbuilderfactory. newinstance (); //  Create a DOM parser factory  10 Documentbuilder = factory. newdocumentbuilder (); // Create a DOM parser  11 Document document = documentbuilder. parse (inputstream ); //  Parse the XML document and obtain the document object corresponding to the XML document.  12 Element rootelement = Document. getdocumentelement (); //  Get the root node in the XML document <persons>  13 Nodelist = rootelement. getelementsbytagname ("person "); //  Obtain all child elements contained in the root node <person>  14          15 List <person> List = New Arraylist <person> ();  16           For ( Int I = 0; I <nodelist. getlength (); I ++ ){ //  Traverse all <person> labels  17 Element element = (element) nodelist. item (I ); //  Obtain each <person> Element Object  18 Person =New  Person ();  19 Person. setid (integer. parseint (element. getattribute ("ID "))); //  Obtain the ID attribute of the <person> element and store it to the person object.  20 Nodelist childnodelist = element. getchildnodes (); //  Obtain all child elements under <person>.  21               For ( Int J = 0; j <childnodelist. getlength (); j ++ ){ // Traverse all sub-tags under the <person> tag  22 Node childnode = (Node) childnodelist. Item (j );  23                   If (Childnode. getnodetype () = node. element_node ){ //  The current node is an element node.  24 Element childelement = (Element) childnode;  25                       If (Childelement. getnodename (). Equals ("name ")){ // Obtain the content in the <Name> label and store it to the person object.  26   Person. setname (childelement. getfirstchild (). getnodevalue ());  27 } Else   If (Childelement. getnodename (). Equals ("Age ")){ //  Obtain the content in the <age> label and save it to the person object.  28   Person. setage (integer. parseint (childelement. getfirstchild (). getnodevalue ()));  29  }  30   }  31   }  32 List. Add (person ); //  Each time a <person> tag is parsed, the person object is saved to the person Object List.  33   }  34           Return  List;  35 }

 

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.