How Android uses DOM to parse XML files

Source: Internet
Author: User

For the following XML file:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE plist Public "-//apple//dtd plist 1.0//en" "Http://www.apple.com/DTDs/PropertyList-1.0.dtd" ><books>      <book email= "Zhoujunhui" >          <name>rjzjh</name>          <price>jjjjjj</price>      </book>       <book email= "aaaaaaaaa" >          <name>bbbb</name>          <PRICE>CCC </price>      </book>  </books>  

This is to parse:

Import Java.util.arraylist;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 Android.app.activity;import Android.content.Context;import Android.content.intent;import Android.content.res.assetmanager;import Android.os.bundle;import Android.util.Log; public class Testactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); XmlReader (); for (int i=0;i<list.size (), i++) {Book book = List.get (i); LOG.D ("TAG", "Name=" +book.name+ "email=" +book.email);}} public void XmlReader () {Document doc = null; Assetmanager Assetmanager = this.getassets ();D ocumentbuilder docbuilder = null;documentbuilderfactory Docbuilderfactory = null;try {docbuilderfactory = documentbuilderfactory.newinstance ();d Ocbuilder = Docbuilderfactory.newdocumentbuilder ();d oc = Docbuilder.parse (Assetmanager.open ("Test1").XML ")); Element root = Doc.getdocumentelement (); NodeList books = Root.getchildnodes (), if (books! = null) {for (int i = 0; i < books.getlength (); i++) {Node book = Book S.item (i); Book MyBook = Null;if (book.getnodetype () = = Node.element_node) {//Here This is the <book> node MyBook = new book (); String email = book.getattributes (). getNamedItem ("email"). Getnodevalue (); mybook.email = email;for (Node node = Book.getfirstchild (); Node! = NULL; node = node.getnextsibling ()) {if (node.getnodetype () = = Node.element_node) {if (Node.getnodename (). Equals ("name")) { String name1 = Node.getfirstchild (). Getnodevalue (); mybook.name = name1;} if (Node.getnodename () Equals ("price")) {String price = Node.getfirstchild (). Getnodevalue (); mybook.price = Price;}}} List.add (MyBook);}}} catch (Exception e) {e.printstacktrace ();}} Private arraylist<book> list = new arraylist<book> ();p rivate class book{string email; String name; String Price;


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.