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;

)

)
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Android - xml parsing (Dom)

  • Spring + Hibernate to use Glassfish Database Connection Pool

    applicationContext.xml file content <? xml version = "1.0" encoding = "UTF-8"?> <beans xmlns = " http://www.springframework.org/schema/beans " xmlns: xsi = " http://www.w3.org/2001/XMLSchema-instance " ...

  • Process migration from tomcat to websphere changes

    Process migration from tomcat to websphere changes Because customers use the web application server software used by different what tomcat5, tomcat6, websphere5.1, websphere6.1, weblogic8, and so on, and the software used inconsistent standards, ibm's

  • JAVA EE JSP_JNDI

    dsfdsa http://lindows.javaeye.com/admin/blogs/213348 Tomcat 6 with the connection pool data source configuration http://www.blogjava.net/ec2008/archive/2008/07/19/216063.html project: test Driver path: D: \ workspace \ test \ WebRoot \ WEB-INF \ lib ...

  • Hibernate.cfg.xml configuration file (including the primary key generation strategy Introduction)

    Hibernate.cfg.xml configuration file: <? xml version = "1.0" encoding = "utf-8"?> <! DOCTYPE hibernate-configuration PUBLIC "- / / Hibernate / Hibernate Configuration DTD / / EN" "hibernate-configuration-2.0.dtd

  • Learn Java flow

    Related Articles: J2EE without EJB Introducing to Spring Framework (English revised edition) J2EE without EJB caused consider Recommend circles: reading space More related recommend Java Learning Path (1), tools, articles First, JDK (Java Development Kit)

  • The EJB3 Persistence

    EJB3 persistence with Hibernate is very similar to the mechanism: Environment: Server: JBOSS5.0 Database: MySQL5.0 1. Set up a data source First of all, in jboss-5.0.0.GA \ server \ default \ deploy, the establishment of a database used to connect the dat

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Spring2.0 + hibernate3.1 + log4j + mysql demo

    applicationContext.xml Non-attachment jar package, necessary friends can send an email to todd.liangt @ gmail.com

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries