Android - xml parsing (pull)

Need to resolve the Xml data prototype is:
<? Xml version = "1.0" encoding = "UTF-8"?>
<name> Li </ name>
<age> 30 </ age>
</ Person>
<name> Li Xiangmei </ name>
<age> 25 </ age>
</ Person>
</ Persons>



import java.util.ArrayList;
import java.util.List;

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

import android.util.Xml;

/ **
* 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> ();

/ / 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 ()));

/ / 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;


/ / End of the current parsing position, point to the next position
type = ();
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 ();


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


  • spring + JdbcTemple + dbcp xml data source implementation 2011-01-06

    In this case, provide us with spring jdbc template to be developed on the spring. With the spring templates, the program no longer there to manage the data connection. Bean are all the way through the automatic scanning injection. This can effectivel

  • [Report] XSLT parsing XML generated form common type XML (1) 2010-06-22

    Requirements Requirements: According to the data out of database queries, export report, spreadsheet format, you can use MS Office to open. The beginning of the idea: 1. Using Apache's POI API to query the data to fill in the Excel document, but thou

  • 1 query table field of type XML in the xml tag for the attribute values 2011-08-26

    ============== 1. Query table field of type XML in the xml tag for the attribute value === <ArgXML Type="Trans"> <attribute name="type" value="pdf" description="" extend="" /> <attribute nam

  • The data type javascript. Object and Function 2011-01-03

    1. Data Types javascript contains six kinds of data types: undefined, null, string, number, boolean, and object. These, the first 5 of the original data type, object is the object type. object types, including Object, Function, String, Number, Boolea

  • 4. Reference type (local object) 2010-10-07

    Create object Like the new operator to instantiate the object with the name created: var o = new Object(); If there are no parameters, parentheses can be omitted: var o = new Object; Can also be used to create an object the following way: var o = {};

  • java sorting function in the implementation of List 2010-06-22

    The time at work today, sort of problems encountered in the List, it summed up a moment to share with you. Collections.sort sort of time, use the Comparator interface to the following compare () method. The following small example, also use the anony

  • java data type, hibernate data type, sql type of data standards for correspondence tables between 2009-08-22

    Java data type Hibernate Data Type Standard SQL data types (PS: for different DB may vary) byte, java.lang.Byte byte TINYINT short, java.lang.Short short SMALLINT int, java.lang.Integer integer INGEGER long, java.lang.Long long BIGINT float, java.lan

  • Use the constructor to determine the type of object properties 2010-04-14

    Use the constructor property to determine the type of object: / / Check whether we are in fact the number of strings if (num.constructor == String) / / If it is, under the string to resolve the integer num = parseInt (num); / / Check whether we are i

  • According to the type name for the type of object 2010-08-23

    private Type GetTypeCore(string typeName) { #if SILVERLIGHT Assembly a = typeof(System.Action).Assembly; Type type = a.GetType(typeName, false); if (type != null) return type; foreach (System.Windows.AssemblyPart ap in System.Windows.Deployment.Curre

  • Java methods to read and write Word doc file (POI code sample) 2010-04-26

    Said lazy to promote social progress. My starting point is to be a tool to automatically generate daily log. Idea is to read google calendar in my daily work records, filling the prescribed form of the word generation company file, and then call the

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 黔ICP备15002463号-1

processed in 0.606 (s). 10 q(s)