Implementation _android of XML parsing and saving based on Android

Source: Internet
Author: User
Tags save file

Parse XML file:
On the Android platform, you can use the pull parser that comes with sax, Dom, and Android to parse XML files;
The pull parser provides a variety of events, uses the Parser.next () method to enter the next element and triggers the corresponding event, and the event generated by the pull parser is a number that can be handled by the switch to the event of interest; through Paser.nexttext () method to get the value of a node of the next text type;
http://xmlpull.org/xmlpull-website/impls.shtml;

Reading XML
Xmlpullparser Pullparser = Xml.newpullparser ();
Pullparser.setinput (XML, "UTF-8");//Add XML data for the cracker to parse
int event = Pullparser.geteventtype ();//start reading, get event return value
Pullparser.getname ()//Get node name
Pullparser.getattributevalue (0);//Get the value of the first property
Pullparser.nexttext ()///Get the contents of the node after the label
event = Pullparser.next ();//The parser encounters an end tag that does not automatically parse down, requiring this method to be called to continue execution;

Save file to XML

public static void Save (list<person> persons, OutputStream OutStream)
Throws Exception, IllegalStateException, IOException {
XmlSerializer serializer = Xml.newserializer ();//Get serialization object for XML write information
Serializer.setoutput (OutStream, "UTF-8");/set the OutputStream to write to
Serializer.startdocument ("UTF-8", true);/Set Document label

Serializer.starttag (NULL, "persons");/set start tag, first parameter is namespace
for (person Person:persons) {
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");
Serializer.enddocument ();
Outstream.flush ();
Outstream.close ();
}

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.