Android basics 09: XML file parsing 02 dom

Source: Internet
Author: User

This article describes how to parse XML in Android.

Android basics 09: XML file parsing 01 Sax

Android basics 09: XML file parsing 02 dom

Android basics 09: XML file parsing 03 pull

In this section, we use the DOM method for processing.

The dom-based XML parsing method first reads all XML documents into the memory, and then uses the dom api to access the tree structure and obtain data. But what if the XML file is large? Of course, the mobile phone's CPU processing capabilities cannot be compared with PC machines, so the processing efficiency is relatively poor. Of course, this is for other methods to process XML documents.

So what should we do?
The specific ideas are as follows:
* First, use documentbuilderfactory to create a documentbuilderfactory instance.
* Use documentbuilderfactory to create documentbuilder.
* Then load the XML document ),
* Then obtain the root node (element) of the document ),
* Obtain the list (nodelist) of all child nodes in the root node ),
* Obtain the nodes to be read from the subnode list.

Next we will start to read the XML Document Object and add it to the list:CodeAs follows:
Here we use the river. xml file in assets, so we need to read this XML file and return the input stream.
The read method is: inputstream = This. Context. getresources (). getassets (). Open (filename); the parameter is the XML file path. Of course, the default assets directory is the root directory.
Then, you can use the parse method of the documentbuilder object to parse the input stream, return the document object, and traverse the node attributes of the doument object.

Public class domxml {// get all river data/*** parameter filename: the XML document path */public list <river> getriversfromxml (string filename, context) {list <river> rivers = new arraylist <river> (); documentbuilderfactory factory = NULL; documentbuilder builder = NULL; document = NULL; inputstream = NULL; // first find the XML file factory = documentbuilderfactory. newinstance (); try {// find the XML and load the document builder = factory. newdocumentbuilder (); inputstream = context. getresources (). getassets (). open (filename); document = builder. parse (inputstream); // find the root element root = document. getdocumentelement (); nodelist nodes = root. getelementsbytagname (Contant. river); // traverses all the child nodes of the root node, and all the river rivers under rivers = NULL; For (INT I = 0; I <nodes. getlength (); I ++) {river = New River (); // gets the river Element Node element riverelement = (element) (nodes. item (I); // gets the name attribute value River in the river. setname (riverelement. getattribute (Contant. name); River. setlength (integer. parseint (riverelement. getattribute (Contant. length); // obtain the introduction label element introduction = (element) riverelement under the river. getelementsbytagname (Contant. introduction ). item (0); River. setintroduction (introduction. getfirstchild (). getnodevalue (); element imageurl = (element) riverelement. getelementsbytagname (Contant. imageurl ). item (0); River. setimageurl (imageurl. getfirstchild (). getnodevalue (); rivers. add (river) ;}} catch (ioexception e) {e. printstacktrace ();} catch (saxexception e) {e. printstacktrace ();} catch (parserconfigurationexception e) {e. printstacktrace ();} finally {try {inputstream. close ();} catch (ioexception e) {e. printstacktrace () ;}} return rivers ;}}

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: 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.