Android XML file read/write
<? Xml version = "1.0" encoding = "UTF-8"?>
<Persons>
<Person id = "23">
<Name> eboy </name>
<Age> 22 </age>
</Person>
<Person id = "24">
<Name> Frr </name>
<Age> 20 </age>
</Person>
</Persons>
Package com. eboy. service;
Import java. io. InputStream;
Import java. io. OutputStream;
Import java. util. ArrayList;
Import java. util. List;
Import org. xmlpull. v1.XmlPullParser;
Import org. xmlpull. v1.XmlSerializer;
Import com. eboy. domain. Person;
Import android. util. Xml;
Public class PersonService {
Public static List <Person> getPersons (InputStream xml) throws Exception {
List <Person> persons = null;
Person person = null;
XmlPullParser pullParser = Xml. newPullParser ();
PullParser. setInput (xml, "UTF-8"); // set the XML data to be parsed for the Pull Interpreter
Int event = pullParser. getEventType ();
While (event! = XmlPullParser. END_DOCUMENT ){
Switch (event ){
Case XmlPullParser. START_DOCUMENT:
Persons = new ArrayList <Person> ();
Break;
Case XmlPullParser. START_TAG:
If ("person". equals (pullParser. getName ())){
Int id = Integer. valueOf (pullParser. getAttributeValue (0 ));
Person = new Person ();
Person. setId (id );
}
If ("name". equals (pullParser. getName ())){
String name = pullParser. nextText ();
Person. setName (name );
}
If ("age". equals (pullParser. getName ())){
Int age = Integer. valueOf (pullParser. nextText ());
Person. setAge (age );
}
Break;
Case XmlPullParser. END_TAG:
If ("person". equals (pullParser. getName ())){
Persons. add (person );
Person = null;
}
Break;
}
Event = pullParser. next ();
}
Return persons;
}
/**
* Save the data to an xml file.
* @ Param persons
* @ Param out
* @ Throws Exception
*/
Public static void save (List <Person> persons, OutputStream out) throws Exception {
XmlSerializer serializer = Xml. newSerializer ();
Serializer. setOutput (out, "UTF-8 ");
Serializer. startDocument ("UTF-8", true );
Serializer. startTag (null, "persons ");
For (Person person: persons ){
Serializer. startTag (null, "person ");
Serializer. attribute (null, "id", person. getId (). toString ());
Serializer. startTag (null, "name ");
Serializer. text (person. getName (). toString ());
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 ();
Out. flush ();
Out. close ();
}
}
/Files/jxgxy/com.eboy.readwritexml.MainActivity.rar
Reprinted address
Http://www.cnblogs.com/jxgxy/archive/2012/07/26/2609730.html
<? Xml version = "1.0" encoding = "UTF-8"?>
<Persons>
<Person id = "23">
<Name> eboy </name>
<Age> 22 </age>
</Person>
<Person id = "24">
<Name> Frr </name>
<Age> 20 </age>
</Person>
</Persons>
Package com. eboy. service;
Import java. io. InputStream;
Import java. io. OutputStream;
Import java. util. ArrayList;
Import java. util. List;
Import org. xmlpull. v1.XmlPullParser;
Import org. xmlpull. v1.XmlSerializer;
Import com. eboy. domain. Person;
Import android. util. Xml;
Public class PersonService {
Public static List <Person> getPersons (InputStream xml) throws Exception {
List <Person> persons = null;
Person person = null;
XmlPullParser pullParser = Xml. newPullParser ();
PullParser. setInput (xml, "UTF-8"); // set the XML data to be parsed for the Pull Interpreter
Int event = pullParser. getEventType ();
While (event! = XmlPullParser. END_DOCUMENT ){
Switch (event ){
Case XmlPullParser. START_DOCUMENT:
Persons = new ArrayList <Person> ();
Break;
Case XmlPullParser. START_TAG:
If ("person". equals (pullParser. getName ())){
Int id = Integer. valueOf (pullParser. getAttributeValue (0 ));
Person = new Person ();
Person. setId (id );
}
If ("name". equals (pullParser. getName ())){
String name = pullParser. nextText ();
Person. setName (name );
}
If ("age". equals (pullParser. getName ())){
Int age = Integer. valueOf (pullParser. nextText ());
Person. setAge (age );
}
Break;
Case XmlPullParser. END_TAG:
If ("person". equals (pullParser. getName ())){
Persons. add (person );
Person = null;
}
Break;
}
Event = pullParser. next ();
}
Return persons;
}
/**
* Save the data to an xml file.
* @ Param persons
* @ Param out
* @ Throws Exception
*/
Public static void save (List <Person> persons, OutputStream out) throws Exception {
XmlSerializer serializer = Xml. newSerializer ();
Serializer. setOutput (out, "UTF-8 ");
Serializer. startDocument ("UTF-8", true );
Serializer. startTag (null, "persons ");
For (Person person: persons ){
Serializer. startTag (null, "person ");
Serializer. attribute (null, "id", person. getId (). toString ());
Serializer. startTag (null, "name ");
Serializer. text (person. getName (). toString ());
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 ();
Out. flush ();
Out. close ();
}
}
/Files/jxgxy/com.eboy.readwritexml.MainActivity.rar
Reprinted address