Android Development (II)-parsing XML files in the PULL mode

Source: Internet
Author: User

Parsing principles of the Pull mode:
Similar to the simulation of the SAX parser, a similar event driver is also provided. You can use parser. next () to enter the next element and trigger the corresponding event. The event is sent as a numeric code, so you can use a switch to process the event you are interested in. When parsing an element, call parser. nextText () to obtain the value of the next Text element.
Steps for parsing the Pull mode:
1. Get an XmlPullParser object. The XMLPullParser object can be obtained in two ways:
1) Use the. newPullParser () method of the Xml tool class to obtain
2) create an object through XmlPullParserFactory
// Obtain the XmlPullParserFacotry Factory

XmlPullParserFactory factory = XmlPullParserFactory. newInstance ();
 
XmlPullParser xmlparser = factory. newPullParser ();

2. Set the input stream and encoding format based on the XmlPullParser object.

Parser. setInput (inputStream, "UTF-8 ");

3. traverse all nodes cyclically. You can use the code at the beginning of the document to determine which node to read.
 
Xmlparser. getEventType () has the following values:
Start_DOCUMENT: Document start
END_DOCUMENT: End of the document
START_TAG: Start read tag
END_TAG: tag end
TEXT: Read TEXT content
 
 
A simple example:

Import java. io. IOException;
Import java. io. StringReader;
Import org. xmlpull. v1.XmlPullParser;
Import org. xmlpull. v1.XmlPullParserException;
Import org. xmlpull. v1.XmlPullParserFactory;
Public class SimpleXmlPullApp {
Public static void main (String args []) throws XmlPullParserException, IOException {
 
XmlPullParserFactory factory = XmlPullParserFactory. newInstance ();
Factory. setNamespaceAware (true );
XmlPullParser xpp = factory. newPullParser ();
 
Xpp. setInput (new StringReader ("<foo> Hello World! </Foo> "));
 
Int eventType = xpp. getEventType ();
 
While (eventType! = XmlPullParser. END_DOCUMENT ){
 
If (eventType = XmlPullParser. START_DOCUMENT ){
 
System. out. println ("Start document ");
 
} Else if (eventType = XmlPullParser. START_TAG ){
 
System. out. println ("Start tag" + xpp. getName ());
 
} Else if (eventType = XmlPullParser. END_TAG ){
 
System. out. println ("End tag" + xpp. getName ());
 
} Else if (eventType = XmlPullParser. TEXT ){
 
System. out. println ("Text" + xpp. getText ());
 
}
EventType = xpp. next ();
}
System. out. println ("End document ");
}
}
Output result:
Start document
Start tag foo
Text Hello World!
End tag foo
For example, read the content in the XML file and construct the content into a Person object. Then read the content in the XML file and add the Person object to a List set.
Person. xml:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Persons>
<Person id = "23">
<Name> Li Ming </name>
<Age> 30 </age>
</Person>
<Person id = "20">
<Name> Li xiangmei </name>
<Age> 25 </age>
</Person>
</Persons>
Person class:

Public class Person {
 
Private Integer id;
Private String name;
Private short age;
Public Integer getId (){
Return id;
}
Public void setId (Integer id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public short getAge (){
Return age;
}
Public void setAge (short age ){
This. age = age;
}
@ Override
Public String toString (){
Return "Person [age =" + age + ", id =" + id + ", name =" + name + "]";
}
}
Business bean

Public static List <Person> readXml (InputStream inputStream)
 
Throws Exception {
 
List <Person> persons = null;
 
// XMLPullParser object
 
XmlPullParser parser = Xml. newPullParser ();
 
// Set the input stream and encoding
 
Parser. setInput (inputStream, "UTF-8 ");
 
// File start code
 
Int eventCode = parser. getEventType ();
 
Person person = null;
 
While (eventCode! = XmlPullParser. END_DOCUMENT ){
 
Switch (eventCode ){
 
// Start the document
 
Case XmlPullParser. START_DOCUMENT:
 
// Initialization
 
Persons = new ArrayList <Person> ();
 
Break;
 
Case XmlPullParser. START_TAG: // start tag
 
If ("person". equals (parser. getName ())){
 
Person = new Person ();
 
Person. setId (Integer. parseInt (parser. getAttributeValue (0 )));
 
}
 
Else if (person! = Null ){
 
If ("name". equals (parser. getName ())){
 
// NextNext () is applicable when the content of the next element is text.
 
Person. setName (parser. nextText ());
 
}
Else if ("age". equals (parser. getName ())){
Person. setAge (new Short (parser. nextText ()));
 
}
}
Break;
Case XmlPullParser. END_TAG: // end tag
 
If ("person". equals (parser. getName () & person! = Null ){
 
Persons. add (person );
 
Person = null; // returns null.
 
}
 
Break;
 
}
// Next node www.2cto.com
EventCode = parser. next ();
}
Return persons;
}

 
 
Test:

Public class SAXPersonServiceTest extends AndroidTestCase {
 
Private static final String TAG = "LogTest ";
 
// PULL Mode
Public void testPULLReadXml () throws Exception {
 
System. out. println ("testPULLReadXml ");
 
InputStream inputStream = SAXPersonServiceTest. class. getClassLoader ()
 
. GetResourceAsStream ("person. xml ");
 
List <Person> list = PULLPersonService. readXml (inputStream );
 
System. out. println (list. size ());

For (Person person: list ){
System. out. println (person );
}

}
 
}

From jiahui524 Column
 

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.