<? 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 = 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: 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