XML parsing Technology in Android (2) Dom parsing XML files

Source: Internet
Author: User

Dom (Document Object Model.

The process of parsing XML in Dom mode is to first read all XML documents to the memory, construct a tree structure, and then allow Dom APIs to operate the entire tree structure, including traversing the XML tree and retrieving the required data.

The Android platform uses APIs in the Java SDK to parse the DOM method. The specific method is similar to the method used in the Java SDK to parse XML. Therefore, it has the basis of the previous sax, the Dom learning method is simple. The main difference between Dom and the former Sax is the structure of the API. Sax is composed of event-based callback, which parses and generates events to call the callback function of the event processor for processing. Dom is not. Dom directly reads the entire XML tree and saves it as a document class. Therefore, you only need to process the Document Object stored in the memory. There is no callback function.

The packages related to Dom parsing are javax. xml. parsers and org. W3C. Dom. Javax. XML. parsers provides the document constructor factory documentbuilderfactory and document constructor documentbuilder. The documentbuilderfactory instance calls newdocumentbuilder () to create a documentbuilder instance, then, the instance can call the parse method to return an instance parsed as a document class. Document
A class is composed of nodes that indicate elements, attributes, and text content of an XML document. Therefore, with the document object, you can obtain the child nodes and their related values in the XML document as needed. Document, element, node, and other dom-related details are stored in org. w3C. in the DOM package, so like sax, org. w3C. the Dom package is the underlying content responsible for Dom parsing and serves as the upper-layer javax. XML. the parsers package provides Dom parser and other related calls.

Dom parsing process:

1. Construct the documentbuilder parser

2. input the XML file input stream, parse the XML file, and return the Document Object.

3. retrieve data from memory based on the document object.

The simple XML file test. XML is still parsed below

<?xml version="1.0" encoding="UTF-8"?><persons><person id="1"><name>Lucy</name><age>15</age></person><person id="2"><name>Tim</name><age>20</age></person></persons>

Public void domparsexml () throws parserconfigurationexception, saxexception, ioexception {// construct the documentbuilder parser documentbuilderfactory builderfactory = documentbuilderfactory. newinstance (); documentbuilder dbuilder = builderfactory. newdocumentbuilder (); // obtain the input stream of the XML file, and obtain the object inputstream = getclass (). getclassloader (). getresourceasstream ("test. XML "); document = dbuilder. parse (inputstream); // The parsing is complete, and the data element root = Document is retrieved from the memory based on the document. getdocumentelement (); // get the document root node (the XML file only allows one root node) nodelist personsnode = root. getelementsbytagname ("person"); // retrieve the list of all person nodes // nodelist personsnode = root. getchildnodes (); this method cannot be used to obtain the person node in the previous step because the child node includes the element node and the text node. If this method is used, you also need to determine the node type list <person> Persons = new arraylist <person> (); For (INT I = 0; I <personsnode. getlength (); I ++) {person = new person (); element personelement = (element) personsnode. item (I); // The person node int id = new INTEGER (personelement. getattribute ("ID"); // gets the property idperson of the person node. setid (ID); nodelist childsnode = personelement. getchildnodes (); // get all subnodes of the person node for (Int J = 0; j <childsnode. getlength (); j ++) {If (childsnode. item (j ). getnodetype () = node. element_node) {// if the child node is an element node element childelement = (element) childsnode. item (j); // obtain this element node if (childelement. getnodename (). equals ("name") {// determines which element node is person. setname (childelement. getfirstchild (). getnodevalue (); // obtain the value of the first subnode of the element node} If (childelement. getnodename (). equals ("Age") {person. setage (New INTEGER (childelement. getfirstchild (). getnodevalue () ;}} persons. add (person); person = NULL;} For (person P: Persons) {system. out. println (P. GETID () + "--" + P. getname () + "---" + P. getage ());}}

Although Dom parsing technology is relatively intuitive, it needs to be installed in a real XML file, which consumes a lot of memory, so it is used less.

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.