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