这里简单些一下xml Pull解析器用法, 直接贴代码:
公共类Person:
package com.xiaoming.domain; public class Person { private int id; private String name; private int score; public Person(){} public Person(int id, String name, int score) { this.id = id; this.name = name; this.score = score; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", score=" + score + "]"; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } }
业务处理类 PersonService:
package com.xiaoming.service; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Writer; import java.util.ArrayList; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlSerializer; import android.util.Xml; import com.xiaoming.domain.Person; public class PersonService { public ArrayList<Person> getPersonList(InputStream in) throws Exception { ArrayList<Person> personList = null; Person tempPerson = null; XmlPullParser pullParser = Xml.newPullParser(); pullParser.setInput(in, "UTF-8"); int event = pullParser.getEventType(); while( XmlPullParser.END_DOCUMENT != event ) { switch(event) { case XmlPullParser.START_DOCUMENT: personList = new ArrayList<Person>(); break; case XmlPullParser.START_TAG: if( "person".equals(pullParser.getName()) ) { tempPerson = new Person(); tempPerson.setId(Integer.parseInt(pullParser.getAttributeValue(0))); } if( "name".equals(pullParser.getName())) { tempPerson.setName(pullParser.nextText()); } if( "score".equals(pullParser.getName())) { tempPerson.setScore(Integer.parseInt(pullParser.nextText())); } break; case XmlPullParser.END_TAG: if( "person".equals(pullParser.getName())) { personList.add(tempPerson); } break; case XmlPullParser.END_DOCUMENT: break; default: } event = pullParser.next(); } return personList; } public void SavePersonList(ArrayList<Person> personList,OutputStream outputstream) throws Exception { XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(outputstream, "UTF-8"); serializer.startDocument("UTF-8", true); serializer.startTag(null, "persons"); for( Person p: personList) { serializer.startTag(null, "person"); serializer.attribute(null, "id", ""+p.getId()); serializer.startTag(null, "name"); serializer.text(p.getName()); serializer.endTag(null, "name"); serializer.startTag(null, "score"); serializer.text(""+p.getScore()); serializer.endTag(null, "score"); serializer.endTag(null, "person"); } serializer.endTag(null, "persons"); serializer.flush(); outputstream.flush(); outputstream.close(); } }
测试类:
package com.xiaoming.test; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import com.xiaoming.service.PersonService; import com.xiaoming.domain.Person; import android.os.Environment; import android.test.AndroidTestCase; import android.util.Log; public class PullParseTest extends AndroidTestCase { private static final String TAG="PullParseTest"; public void testGetPersonList() throws Exception { InputStream xml = this.getClass().getClassLoader().getResourceAsStream("person.xml"); ArrayList<Person> personList = null; PersonService p = new PersonService(); personList = p.getPersonList(xml); for( Person person: personList) { Log.i(TAG, person.toString()); } } public void testSavePersonList() throws Exception { String path = Environment.getExternalStorageDirectory().getPath(); String fileName = path+"/"+"person.xml"; File file = new File(fileName); if( !file.exists() ) { file.createNewFile(); } OutputStream fout = new FileOutputStream(file); PersonService psersonserver = new PersonService(); ArrayList<Person> plist = new ArrayList<Person>(); Person p1 = new Person(1,"xiaoming",1); Person p2 = new Person(2,"张亮",2); plist.add(p1); plist.add(p2); psersonserver.SavePersonList(plist, fout); } }
person.xml文件:
<?xml version="1.0" encoding="utf-8"?> <persons> <person id="1"> <name>小明</name> <score>100</score> </person> <person id="2"> <name>张小</name> <score>100</score> </person> <person id="3"> <name>晕</name> <score>900</score> </person> </persons>