Parsing XML files:
On the Android platform, you can use the pull parser included with SAX, DOM, and Android to parse XML files;
The pull parser provides various events, using the() method to enter the next element and trigger the corresponding event. The event generated by the pull parser is a number, which can be processed through switch; the value of the next Text type node can be obtained through the() method;
/xmlpull-website/;
Read XML
XmlPullParser pullParser = ();
(xml, "UTF-8");//Add XML data to parse for the cracker
int event = ();//Start read and get the event return value
();//Get the node name
(0);//Get the value of the first attribute
();//Get the content of the node after the tag
event = ();// When the parser encounters the end tag, it will not automatically parse downwards, and it is necessary to call this method to continue execution;
Save file to XML
public static void save(List<Person> persons, OutputStream outStream)
throws Exception, IllegalStateException, IOException {
XmlSerializer serializer = ();//Get the serialized object for XML write information
(outStream, "UTF-8");//Set the OutputStream to be written
("UTF-8", true);//Set the document label
(null, "persons");//Set the start tag, the first parameter is namespace
for (Person person : persons) {
(null, "person");
(null, "id", ().toString());
(null, "name");
(());
(null, "name");
(null, "age");
(().toString());
(null, "age");
(null, "person");
}
(null, "persons");
();
();
();
}