Android - xml parsing (pull)

sponsored links
Need to resolve the 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.pull.service;

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

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;
import cn.android.domain.Person;

/ **
* Android way to use pull parsing and creating xml implementation class
*
* @ Author Administrator
*
* 2010-6-29 10:02:45 PM
* /
public class XmlPaseService (

/ **
* Pull parsing xml realization mode
*
* @ Param is
* Xml input stream form
After parsing * @ return the object to return a collection of
* @ Throws Exception
* /
public static List <Person> parseXml (InputStream is) throws Exception (

/ / Create the pull parsing object
XmlPullParser parser = Xml.newPullParser ();

/ / The xml file to the form of the input stream object is passed to the pull parsing, parsing the object and set the encoding according to utf-8 parse
parser.setInput (is, "utf-8");

/ / Pull parsing parsing xml object, it will return a value representative of position analysis.
/ / START_DOCUMENT: start parsing the document
/ / START_TAG: start parsing tags
/ / END_TAG: tag end
/ / END_DOCUMENT: xml parsing the end of the document
/ / Get the current parsing position
int type = parser.getEventType ();

Person person = null;
List <Person> personList = null;

/ / When there is no parsing of the document at the end of time, has been implementing
while (type! = XmlPullParser.END_DOCUMENT) (

/ / Switch the location of analysis
switch (type) (
/ / Start parsing the file, initialize the object collection
case XmlPullParser.START_DOCUMENT:
personList = new ArrayList <Person> ();
break;

/ / Start parsing when tag, according to label the different names. Do different operations
case XmlPullParser.START_TAG:
/ / Tag for the person's time, remove the tag id attribute value, and save it to Person object in
if ("person". equals (parser.getName ())) (
person = new Person ();
person.setId (new Integer (parser.getAttributeValue (0)));
) Else if (person! = Null) (
/ / When the label name, remove the label inside the body of the text value, and save it to Person object in
if ("name". equals (parser.getName ())) (
person.setName (parser.nextText ());
)
/ / When the label of age, take out the bodies inside the text value tag, and save it to an object in Person
if ("age". equals (parser.getName ())) (
person.setAge (new Short (parser.nextText ()));
)
)
break;

/ / When the resolution to label the end of the implementation of the
case XmlPullParser.END_TAG:
/ / If the end of the tag name is the person, then the object will be saved to personList in Person
if ("person". equals (parser.getName ())) (
if (person! = null & & personList! = null) (
personList.add (person);
person = null;
)
)

break;
)

/ / End of the current parsing position, point to the next position
type = parser.next ();
)
is.close ();
/ / Returns array of objects
return personList;
)

/ **
* Use pull method to create a xml
*
* @ Param writer
* Pass an output stream (the need to create xml output after the location)
* @ Param personList
* Set of objects used to generate the xml of the specific data
* @ Return returns generated xml data to form returns a string
* @ Throws Exception
* /
public static String createXml (Writer writer, List <Person> personList) throws Exception (
/ / Create a connection object xml
XmlSerializer serializer = Xml.newSerializer ();
/ / Will need to output the output of exile to the connection object in xml
serializer.setOutput (writer);
/ / Start creating the document
serializer.startDocument ("utf-8", true);
/ / Create the root element of persons
serializer.startTag ("", "persons");
/ / Variable data set
if (personList! = null) (
for (Person person: personList) (
/ / Create person tag
serializer.startTag ("", "person");
/ / To the person tag with id attribute value is out of the Person object traversal id value
serializer.attribute ("", "id", person.getId () + "");
/ / Create name tags
serializer.startTag ("", "name");
/ / Add text elements to the name tag, a value of traversing the object name out of the value of Person
serializer.text (person.getName ());
/ / End of the name tag
serializer.endTag ("", "name");
/ / Create the age labels
serializer.startTag ("", "age");
/ / Add text labels to the age element value traverse out of the age value of Person objects
serializer.text (person.getAge () + "");
/ / End of the age label
serializer.endTag ("", "age");
/ / End person tag
serializer.endTag ("", "person");
)
)
/ / End label persons
serializer.endTag ("", "persons");
/ / End document
serializer.endDocument ();
/ / Flush output buffer
writer.flush ();
/ / Close the output streams
writer.close ();
/ / Create the form of the characters return to the output data stream
return writer.toString ();
)

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

Related Posts of Android - xml parsing (pull)

  • 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

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

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