Android - xml parsing (Dom)

Xml Data prototype is:
<? Xml version = "1.0" encoding = "UTF-8"?>
<persons>
<person>
<name> Li </ name>
<age> 30 </ age>
</ Person>
<person>
<name> Li Xiangmei </ name>
<age> 25 </ age>
</ Person>
</ Persons>
-------------------------------------------------- "

package cn.android.dom;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import cn.android.domain.Person;

/ **
* Dom parsing xml implementation class
* @ Author Administrator
*
* 2010-6-29 08:16:27 PM
* /
public class DomParser (

/ **
* Parsing xml file and return a collection of objects
* @ Param is xml file input stream
* @ Return object collections
* @ Throws Exception
* /
public static List <Person> paseXml (InputStream is) throws Exception (

/ / Create a collection of objects for storage after resolve
List <Person> personList = new ArrayList <Person> ();

/ / Create object reference
Person person = null;

/ / Get Dom parsing object factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ();

/ / By the factory to create object instance Dom Analysis
DocumentBuilder db = factory.newDocumentBuilder ();

/ / The xml file parsing the input stream to the Dom object analysis, and Dom tree to return.
Document document = db.parse (is);

/ / Received through the tree to the root element of Dom
Element rootElement = document.getDocumentElement ();

/ / Get the root element by the name of the person of all subordinate nodes
NodeList nodeList = rootElement.getElementsByTagName ("person");

/ / Traverse the person taking out a collection of nodes
for (int i = 0; i <nodeList.getLength (); i + +) (

/ / Get a person node
Element personElement = (Element) nodeList.item (i);

/ / Create a Person object
person = new Person ();

/ / The xml tags of the Id attribute value assigned to the Person object's Id property
person.setId (new Integer (personElement.getAttribute ("id ")));

/ / Get person's subordinates node label
NodeList personChildList = personElement.getChildNodes ();

/ / Loop to the subordinate label
for (int y = 0; y <personChildList.getLength (); y + +) (

/ / Create a reference point to the label loop
Node node = personChildList.item (y);

/ / If this cycle is out of the elements of Element objects, that label element, then the following code
if (Node.ELEMENT_NODE == node.getNodeType ()) (

/ / If the tag name is name, then get its value, assigned to Person objects
if ("name". equals (node.getNodeName ())) (
String nameValue = node.getFirstChild (). GetNodeValue ();
person.setName (nameValue);
)

/ / If the tag name is age, then get its value, assigned to Person objects
if ("age". equals (node.getNodeName ())) (
String ageValue = node.getFirstChild (). GetNodeValue ();
person.setAge (new Short (ageValue));
)
)

)

/ / This person objects to the personList in
personList.add (person);

/ / The person of the air
person = null;

)

/ / Return the xml parsing set of objects obtained after
return personList;

)

)

分类:Mobile 时间:2010-06-29 人气:220
分享到:
blog comments powered by Disqus

相关文章

  • Access CORBA services in the JSP object instance 2010-07-14

    Combination of J2EE and CORBA can take advantage of the advantages of both performance and better build more powerful applications, for example, use C + + implementation of the CORBA object as a business process components to form a high-performance logic

  • (Original) java xml data access WebService returns an instance of students into the local file 2010-06-17

    Reprinted please indicate the source: http://eric-619.javaeye.com/blog/692838 import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.io.FileNotFou

  • Based on a generic object instance constructor 2010-12-16

    How to create a generic null object instance: default (T) How to create a generic object instance: Activator.CreateInstance (typeof (T)) How to create a dynamic generic object instance: Type listType = typeof (List <>). MakeGenericType (new Typ

  • JAVA XML parsing approach DOM.SAX.DOM4J.JDOM.StAX Illustration and comparison of 2010-06-05

    1. Wapakhabulo in various ways 1) DOM (JAXP Crimson parser) DOM is platform-and language-independent manner, said the official W3C standard XML document. DOM is a hierarchical organization of nodes or pieces of information collection. This hierarchy ...

  • xml file parsing of Dom 2010-04-04

    The following example simple, only for advanced users. Generate XML documents, reference DOM package. test.xml

  • [Transfer] oracle data import and export imp / exp instance 2011-07-04

    oracle data import and export imp / exp instance Category: oracle 2008-09-08 21:07 196 people read comments (0) Add report imp / exp import and export instance: Database configuration is as follows: sid = orcl_192.168.123.123; user: test; password: t

  • org.w3c.dom.Document object and a string Hu Zhuan 2010-03-23

    /** * XML org.w3c.dom.Document Go String */ public static String docToString(Document doc) { // XML Go string String xmlStr = ""; try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.setOutputProperty("enc

  • [Transfer] org.w3c.dom.Document object interchangeable objects org.dom4j.Document 2011-01-08

    /** * org.w3c.dom.Document -> org.dom4j.Document * @param doc Document(org.w3c.dom.Document) * @return Document */ public static Document parse(org.w3c.dom.Document doc) throws Exception { if (doc == null) { return (null); } org.dom4j.io.DOMReader

  • RegExp object instance of the lastIndex property 2010-08-13

    Each instance of RegExp object has lastIndex property, it was a successful search string matches the beginning of the next position (Rhino interpretation of the book "After the last match position for many times in a string matching" ), the

  • java object instance initialization sequence 2011-04-22

    How java object is initialized, the new file as a PrintClass.java (including class PrintClass and A and its subclass B), compile and run one of the main methods public class PrintClass { public static void main (String [] args) { new B (); } PrintCla

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.430 (s). 12 q(s)