Android XML parsing Dom detailed introduction

Source: Internet
Author: User

Reference Address: http://wenku.baidu.com/view/aca83d12cc7931b765ce15d1.html

Because the DOM is stored in memory, it consumes less memory, but for some small but more commonly used hidden information, you can use this method

The People class is a class that writes itself, primarily saving individual string data. 1. For extensibility you have compiled an XML file: <?xml version= "1.0" encoding= "Utf-8"?>    <peoples>        <people            name= "Xie xx" age= "all" >aaaaaaaaa      &N Bsp     <nationality> China </nationality>            <graduation> XXX University </graduation>            <introduction name= "Introduction" > Xie XX Test description of sentence length long ... </introduction>        </people>         <people            name= "King xx" age= "all" >bbbbbbbb            <nationalit Y> China </nationality>            &LT;GRADUATION&GT;XX University </graduation>            <introduction> King xx Test description of sentences </introduction>        </ people>   &nbsP     <people            name= "Lam xx" age= "all" >cccccccc      &NB Sp     <nationality> China </nationality>            <graduation> Polytechnic University </graduation>            <introduction> Forest xx test description of sentences </introduction>         </people>    </peoples>     Summary: Sort of like a tree structure.  2. Key parsing Section code: (1) Declare classes of various needs: Documentbuilderfactory factory = Null;documentbuilder Builder = null;document Document = Null;inputstream inputstream = null; (2) Implement DOM parsing list = new arraylist<people> (); factory = Documentbuilderfactory.newinstance (); the Process builder = Factory.newdocumentbuilder () that obtains the instance of the document file by try {//); InputStream = XmlMainActivity.this.getResources (). Getassets (). Open ("Test.xml");//Use the Assets folder under the project file as the root directory document = Builder.parse ( InputStream);   //gets the root element to list all nodes nodelistelement root = Document.getdocumentelement ();// getElementsByTagName is to look for The "people" flag under the current Element and generate the NodeList NodeList nodes = Root.getelementsbytagname_r ("people"); //the data in each node, it should be divided into 3 kinds of data,//① is the name, the age of attribute data;//② is the nodevalue data outside the <people> brackets;// The ③ is finally the other node in its ground. for (int i = 0; i < nodes.getlength (); i++) {Element Peopleitem = (Element) nodes.item (i); name = Peopleitem.getattribut E ("name"); age = Peopleitem.getattribute ("Age");//Introduction=peopleitem.getfirstchild (). Getnodevalue (); ELEMENT item = (element) Peopleitem.getelementsbytagname_r ("nationality"). Item (0); nationality = Item.getfirstchild () . Getnodevalue ();  item = (Element) peopleitem.getelementsbytagname_r ("Graduation"). Item (0); graduation = Item.getfirstchild (). Getnodevalue ();  item = (Element) peopleitem.getelementsbytagname_r ("Introduction"). Item (0); introduction =item.getfirstchild (). Getnodevalue ();//NodeList Childenodes=peopleitem.getelementsbytagname_r (" Introduction ");//Element childitem= (Element) Childenodes.item (0);//Introduction=childitem.getattribute (" name "); /introduction=introduction+ " " +childitem.getfirstchild (). Getnodevalue ();  list.add(New people (name, age, nationality, graduation,introduction, xmlmainactivity.this));  } } catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}    According to vulgar understanding: DOM parsing process is a bit like tree traversal process, ① get root nodeElement root =document.getdocumentelement ();② Get all node list NodeList nodes = Root.getelementsbytagname_r ("people"); ③ Gets the sub-node Element Peopleitem = (Element) nodes.item (i); ④ Obtaining attribute data,nodevalue data of sub-node I⑤ lists the "Introduction" sub-nodes of the sub-node NodeList childenodes=peopleitem.getelementsbytagname_r (" Introduction "); ⑥ repeat ③④ steps;⑦ lists the "nationality" sub-node NodeList of the child node. ⑧repeat the ③④ step;...⑨get the root directory under I+1 sub -node Element Peopleitem = (Element) nodes.item (i); and Repeat XML is a little bit more complicated, but the picture is very good. Element and document inherit the node interface nodelist itself is just an interface document itself represents the entire XML file, element represents a node, which is represented by the document.getdocumentelement () The element obtained is the root node, then the Root.getelementsbytagname_r ("people") is called from the root node, and all nodelist under different tags can be obtained and then obtained (element) Nodelist.item (i) The child node can then be called at the end of the child node.getElementsByTagName.

The entire parsing process can be not in accordance with the inherent hierarchical relationship of XML, that is, can be directly from the root node to find the nth layer of labels, as long as logically handled properly.

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.