One, pull parse xml
Public StaticList<person> xmlparser (inputstream XML)throwsException {List<Person> persons=NULL; Person P=NULL; Xmlpullparser Parser=Xml.newpullparser (); Parser.setinput (XML,"UTF-8"); intEvent=Parser.geteventtype (); while(event!=xmlpullparser.end_document) { Switch(event) { CaseXmlPullParser.START_DOCUMENT:persons=NewArraylist<person>(); Break; CaseXmlpullparser.start_tag:if(Parser.getname (). Equals ("Person") ) {p=NewPerson (); P.setid (Integer.parseint (Parser.getattributevalue (0))); } if(Parser.getname (). Equals ("name") {p.setname (Parser.nexttext ()); } if(Parser.getname (). Equals ("Age") {p.setage (Integer.parseint (Parser.nexttext ())); } Break; CaseXmlpullparser.end_tag:if(Parser.getname (). Equals ("Person") {persons.add (P); P=NULL; } Break; } Event=Parser.next (); } returnpersons; }
Ii. saving information in XML format
Public Static voidSaveasxml (list<person> persons,outputstream OS)throwsException {XmlSerializer xmlserial=Xml.newserializer (); Xmlserial.setoutput (OS,"UTF-8"); Xmlserial.startdocument ("UTF-8",true); Xmlserial.starttag (NULL, "persons"); for(person person:persons) {Xmlserial.starttag (NULL, "Person"); Xmlserial.attribute (NULL, "id", String.valueof (Person.getid ())); Xmlserial.starttag (NULL, "name"); Xmlserial.text (Person.getname ()); Xmlserial.endtag (NULL, "name"); Xmlserial.starttag (NULL, "Age"); Xmlserial.text (String.valueof (Person.getage ())); Xmlserial.endtag (NULL, "Age"); Xmlserial.endtag (NULL, "Person"); } Xmlserial.endtag (NULL, "persons"); Xmlserial.enddocument (); Os.flush (); Os.close (); }