Android learning notes --- Android platform 1. parse xml files using dom

Source: Internet
Author: User

1. Using dom to parse xml files in addition to using SAX to parse XML files, you can also use the familiar DOM to parse XML files. When DOM parses an XML file, it stores all the content of the XML file in the memory as a document tree, and then allows you to use DOM APIs to traverse the XML tree and retrieve the required data. The code that uses DOM to operate XML looks intuitive, and it is easier to code than to implement it based on SAX. However, because the DOM needs to store all the content of the XML file in the memory as a document tree, the memory consumption is relatively large, especially for mobile devices running Android, because the device resources are precious, we recommend that you use SAX to parse the XML file. Of course, if the content of the XML file is small, DOM is also a feasible code. Please refer to the remarks at the bottom of this page to import java. io. inputStream; import java. util. arrayList; import java. util. list; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import org. w3c. dom. document; import org. w3c. dom. element; import org. w3c. dom. node; import org. w3c. dom. nodeList; import cn. itcast. xml. domain. person;/*** use Dom to parse xml files **/public class DomXMLReader {public static List ReadXML (InputStream inStream ){ List Persons = new ArrayList (); DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); try {DocumentBuilder builder = factory. newDocumentBuilder (); Document dom = builder. parse (inStream); Element root = dom. getDocumentElement (); NodeList items = root. getElementsByTagName ("person"); // find all person nodes for (int I = 0; I <items. getLength (); I ++) {Person person Person = new person (); // obtain the first Person node Element personNode = (Element) items. item (I); // get the id attribute value of the person node person. setId (new Integer (personNode. getAttribute ("id"); // obtain all subnodes under the person node (blank nodes and name/age elements between labels) NodeList childsNodes = personNode. getChildNodes (); for (int j = 0; j <childsNodes. getLength (); j ++) {Node node = (Node) childsNodes. item (j); // determines whether the element type is if (node. getNodeType () = Node. ELEMENT_NODE) {Element childNode = (Element) node; // determine whether the name Element if ("name ". equals (childNode. getNodeName () {// get the Text node under the name element, and then get the data person from the Text node. setName (childNode. getFirstChild (). getNodeValue ();} else if ("age ". equals (childNode. getNodeName () {person. setAge (new Short (childNode. getFirstChild (). getNodeValue () ;}} persons. add (person);} inStream. close ();} catch (Exception e) {e. printStackTrace ();} return persons ;}----------------------------------------------------------

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.