Manipulating the XML of Android via the pull parser

Source: Internet
Author: User

Manipulating the XML of Android via the pull parser

The example defines a javabean for storing the XML content parsed above, the JavaBean is the person, and the code is shown below this page:

==============================================================================================

Parsing a person.xml file using the Pull parser

public class Personservice {

public static List getpersons (InputStream xml) throws exception{ Receives an XML file object
List persons = NULL;
person person = null;
Xmlpullparser parser = Xml.newpullparser (); Get the Xmlpull parser using the Android XML tool class
Parser.setinput (XML, "UTF-8");  parsing files, setting character sets
int event = Parser.geteventtype (); Gets the parsing state, which returns an int type number state
while (event! = xmlpullparser.end_document) {If the status is not an end event end_document, it is recursive
Switch (event) {
Case Xmlpullparser.start_document: Initialize the data if you are start_document to start parsing header tags
persons = new ArrayList ();
Break

Case Xmlpullparser.start_tag:  Gets the data if the property Start_tag is started parsing
if ("Person". Equals (Parser.getname ())) { Gets the data based on the attribute index value if the property name is a set name
Integer PersonID = new Integer (parser.getattributevalue (0));
person = new person (); To create an Object encapsulation property
Person.setid (PersonID);
}else if ("Name". Equals (Parser.getname ())) {
Person.setname (Parser.nexttext ());If the data to get is in text, call the Nexttext () method to get
}else if ("Age". Equals (Parser.getname ())) {
Person.setage (New Short (Parser.nexttext ()));
}
Break
Case Xmlpullparser.end_tag: When the name of the triggering XML document end event is person (that is, the resolved end position), the encapsulated object is placed in the collection, and the object is made empty
if ("Person". Equals (Parser.getname ())) {
Persons.add (person);
person = null;
}
Break
}
event = Parser.next ();  To point the pointer to the next node
}
return persons;
}


*************************************************************************

Creating an XML document from the Pull connector


public static void Save (List persons, OutputStream OutStream) throws exception{
XmlSerializer serializer = Xml.newserializer (); To create a serialized file of XML
Serializer.setoutput (OutStream, "UTF-8"); Incoming saved output stream, and character set encoding
Serializer.startdocument ("UTF-8", true);  XML header tag character set, whether serialized
Serializer.starttag (NULL, "persons"); Start node settings, (namespace null, name persons)
for (person Person:persons) {  Looping through setting properties and data for each node
Serializer.starttag (null, "person");
Serializer.attribute (NULL, "id", Person.getid (). toString ());

Serializer.starttag (NULL, "name");
Serializer.text (Person.getname ());
Serializer.endtag (NULL, "name");

Serializer.starttag (NULL, "age");
Serializer.text (Person.getage (). toString ());
Serializer.endtag (NULL, "age");

Serializer.endtag (null, "person");
}
Serializer.endtag (NULL, "persons"); Set End Node
Serializer.enddocument (); End XML editing, which is to set the end of the XML document
Outstream.flush (); It is best to brush out the data in the cache before closing the output stream in case the incoming stream is the cache stream
Outstream.close ();
}
}

Manipulating the XML of Android via the pull parser

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.