Android 开发工具类 22_PullPersonService
PULL 解析 XML
1 import java.io.InputStream; 2 import java.io.OutputStream; 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.xmlpull.v1.XmlPullParser; 7 import org.xmlpull.v1.XmlSerializer; 8 9 import android.util.Xml; 10 11 import com.wangjialin.domain.Person; 12 13 public class PullPersonService { 14 // 生成 XML 15 public static void save(List<Person> persons, OutputStream outStream) throws Exception{ 16 17 XmlSerializer serializer = Xml.newSerializer(); 18 serializer.setOutput(outStream, "UTF-8"); 19 serializer.startDocument("UTF-8", true); 20 serializer.startTag(null, "persons"); 21 22 for(Person person : persons){ 23 serializer.startTag(null, "person"); 24 serializer.attribute(null, "id", person.getId().toString()); 25 serializer.startTag(null, "name"); 26 serializer.text(person.getName()); 27 serializer.endTag(null, "name"); 28 29 serializer.startTag(null, "age"); 30 serializer.text(person.getAge().toString()); 31 serializer.endTag(null, "age"); 32 33 serializer.endTag(null, "person"); 34 } 35 serializer.endTag(null, "persons"); 36 serializer.endDocument(); 37 outStream.flush(); 38 outStream.close(); 39 } 40 41 public static List<Person> getPersons(InputStream inStream) throws Exception{ 42 43 Person person = null; 44 List<Person> persons = null; 45 XmlPullParser pullParser = Xml.newPullParser(); 46 pullParser.setInput(inStream, "UTF-8"); 47 int event = pullParser.getEventType(); // 触发第一个事件 48 49 while(event != XmlPullParser.END_DOCUMENT){ 50 switch (event) { 51 case XmlPullParser.START_DOCUMENT: 52 persons = new ArrayList<Person>(); 53 break; 54 case XmlPullParser.START_TAG: 55 if("person".equals(pullParser.getName())){ 56 int id = new Integer(pullParser.getAttributeValue(0)); 57 person = new Person(); 58 person.setId(id); 59 } 60 if(person != null){ 61 if("name".equals(pullParser.getName())){ 62 person.setName(pullParser.nextText()); 63 } 64 if("age".equals(pullParser.getName())){ 65 person.setAge(new Short(pullParser.nextText())); 66 } 67 } 68 break; 69 70 case XmlPullParser.END_TAG: 71 if("person".equals(pullParser.getName())){ 72 persons.add(person); 73 person = null; 74 } 75 break; 76 } 77 event = pullParser.next(); 78 } 79 return persons; 80 } 81 }
JUnit 测试
1 public void testPullgetPersons() throws Throwable{ 2 3 InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("wangjialin.xml"); 4 List<Person> persons = PullPersonService.getPersons(inStream); 5 6 for(Person person : persons){ 7 Log.i(TAG, person.toString()); 8 } 9 } 10 11 public void testSave() throws Throwable{ 12 13 File file = new File(this.getContext().getFilesDir(), "wangjialin.xml"); 14 FileOutputStream outStream = new FileOutputStream(file); 15 16 List<Person> persons = new ArrayList<Person>(); 17 persons.add(new Person(90, "wangjialin", (short)13)); 18 persons.add(new Person(35, "jialingege", (short)23)); 19 persons.add(new Person(78, "Android", (short)33)); 20 21 PullPersonService.save(persons, outStream); 22 }
wangjialin.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <persons> 3 <person id = "23"> 4 <name>NEwii</name> 5 <age>21</age> 6 </person> 7 <person id = "20"> 8 <name>WiiWill</name> 9 <age>25</age> 10 </person> 11 </persons>