Android uses pull to parse XML files

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.