Pull parsing xml files, like sax and DOM, can be used separately from Android, pull and sax, and the difference is that pull reads an XML file and then calls the method to return a number.
The declaration read to XML returns the number 0 start_document;
The end of reading to XML returns the number 1 end_document;
The start tag read to XML returns the number 2 Start_tag
The end tag read to XML returns the number 3 End_tag
Text read to XML returns the number 4 text
Pull is the Open-source project source code download address http://www.xmlpull.org/
The code in the service
Package com.leequer.Service;
Import Java.io.InputStream;
Import java.util.ArrayList;
Import java.util.List;
Import Org.xmlpull.v1.XmlPullParser;
Import android.util.Xml;
Import Com.leequer.Doem.Person;
/**
* Parsing XML using pull
* 2010-04-26
* @author Leequer
*
*/
public class Readxmlbypullservice {
public static list<person> Readxmlbypull (InputStream inputstream) throws Exception
{
List <Person> personlist = null;
/**
* Android provides us with XML to get the Xmlpull parser
*/
Xmlpullparser xmlpull = Xml.newpullparser ();
/**
* The input is passed into the set encoding mode
*
*/
Xmlpull.setinput (InputStream, "utf-8");
/**
* Pull reads XML and returns a number
* Read to XML declaration returns the number 0 start_document;
The end of reading to XML returns the number 1 end_document;
The start tag read to XML returns the number 2 Start_tag
The end tag read to XML returns the number 3 End_tag
Text read to XML returns the number 4 text
*/
int eventcode = Xmlpull.geteventtype ();
/**
* As long as this event returns not 1, we'll be reading the XML file.
*/
person person = null;
while (eventcode!=xmlpullparser.end_document)
{
Switch (EventCode)
{
Case Xmlpullparser.start_document:
{//Start document new array
Personlist =new arraylist<person> ();
Break
}
Case Xmlpullparser.start_tag:
{
if ("Person". Equals (Xmlpull.getname ()))
{
person = new person ();
Person.setid (xmlpull.getattributevalue (0));
}else if (person!=null)
{
if (("Name". Equals (Xmlpull.getname ()))
{
/**
* If the next element of the current element is a text node, you can directly use the Nexttext () method to get the contents of the text node
*/
Person.setname (Xmlpull.nexttext ());
}else if ("Age". Equals (Xmlpull.getname ()))
{
/**
* If the next element of the current element is a text node, you can directly use the Nexttext () method to get the contents of the text node
*/
Person.setage (Xmlpull.nexttext ());
}
}
Break
}
Case Xmlpullparser.end_tag:
{
if ("Person". Equals (Xmlpull.getname ()) &&person!=null)
{
Personlist.add (person);
person = null;
}
Break
}
}
EventCode = Xmlpull.next ()//No end XML file is pushed to the next parse
}
return personlist;
}
}