Android应用中使用Pull解析XML文件(传智播客)

来源:互联网 发布:linux删除当前文件夹 编辑:程序博客网 时间:2024/04/29 15:15

在Android应用中使用Pull解析XML文件(传智播客视频笔记)

Service.java源码:

  1. package com.sinaapp.ssun.service;  
  2.   
  3. import java.io.InputStream;  
  4. import java.io.OutputStream;  
  5. import java.util.*;  
  6.   
  7. import org.xmlpull.v1.XmlPullParser;  
  8. import org.xmlpull.v1.XmlPullParserFactory;  
  9. import org.xmlpull.v1.XmlSerializer;  
  10.   
  11. import android.util.Xml;  
  12.   
  13. import com.sinaapp.ssun.domain.Person;  
  14.   
  15. public class Service {  
  16.     /** 
  17.      * 获取XML文件中的数据 
  18.      * @param xml 
  19.      * @return 
  20.      * @throws Exception 
  21.      */  
  22.     public static List<Person> getPersons(InputStream xml) throws Exception {  
  23.         List<Person> persons = null;  
  24.         XmlPullParser parser = XmlPullParserFactory.newInstance()  
  25.                 .newPullParser();  
  26.         // parser = Xml.newPullParser();  
  27.         parser.setInput(xml, "UTF-8");  
  28.         int event = parser.getEventType();  
  29.         Person p = null;  
  30.         while (event != XmlPullParser.END_DOCUMENT) {  
  31.             switch (event) {  
  32.             case XmlPullParser.START_DOCUMENT:  
  33.                 persons = new ArrayList<Person>();  
  34.                 break;  
  35.             case XmlPullParser.START_TAG:  
  36.                 if("person".equals(parser.getName())){  
  37.                     p = new Person();  
  38.                     int id = Integer.parseInt(parser.getAttributeValue(0));  
  39.                     p.setId(id);  
  40.                 }  
  41.                 if("name".equals(parser.getName())){  
  42.                     String name = parser.nextText();  
  43.                     p.setName(name);  
  44.                 }  
  45.                 if("age".equals(parser.getName())){  
  46.                     int age = Integer.parseInt(parser.nextText());  
  47.                     p.setAge(age);  
  48.                 }  
  49.                 break;  
  50.             case XmlPullParser.END_TAG:  
  51.                 if("person".equals(parser.getName())){  
  52.                     persons.add(p);  
  53.                     p = null;  
  54.                 }  
  55.                 break;  
  56.             }  
  57.             event = parser.next();  
  58.         }  
  59.         return persons;  
  60.     }  
  61.       
  62.     /** 
  63.      * 保存数据到XML文件中 
  64.      * @param persons 
  65.      * @param out 
  66.      * @throws Exception 
  67.      */  
  68.     public static void save(List<Person> persons , OutputStream out) throws Exception{  
  69.         XmlSerializer serializer  =  Xml.newSerializer();  
  70.         serializer.setOutput(out, "UTF-8");  
  71.         serializer.startDocument("UTF-8"true);  
  72.         serializer.startTag(null"persons");  
  73.         for(Person p: persons){  
  74.             serializer.startTag(null"person");  
  75.             serializer.attribute(null"person", p.getId()+"");  
  76.               
  77.             serializer.startTag(null"name");  
  78.             serializer.text(p.getName());  
  79.             serializer.endTag(null"name");  
  80.               
  81.             serializer.startTag(null"age");  
  82.             serializer.text(p.getAge()+"");  
  83.             serializer.endTag(null"age");  
  84.               
  85.             serializer.endTag(null"person");  
  86.         }  
  87.         serializer.endTag(null"persons");  
  88.         serializer.endDocument();  
  89.         out.flush();  
  90.         out.close();  
  91.     }  
  92. }  
Person.java源码:

  1. package com.sinaapp.ssun.domain;  
  2.   
  3. public class Person {  
  4.     private String name;  
  5.     private int age;  
  6.     private int id;  
  7.       
  8.     public String getName() {  
  9.         return name;  
  10.     }  
  11.     public void setName(String name) {  
  12.         this.name = name;  
  13.     }  
  14.     public int getAge() {  
  15.         return age;  
  16.     }  
  17.     public void setAge(int age) {  
  18.         this.age = age;  
  19.     }  
  20.     public int getId() {  
  21.         return id;  
  22.     }  
  23.     public void setId(int id) {  
  24.         this.id = id;  
  25.     }  
  26.       
  27.     public Person(String name, int age, int id) {  
  28.         this.name = name;  
  29.         this.age = age;  
  30.         this.id = id;  
  31.     }  
  32.     public Person() {  
  33.         super();  
  34.     }  
  35.     @Override  
  36.     public String toString() {  
  37.         return "Person [name=" + name + ", age=" + age + ", id=" + id + "]";  
  38.     }  
  39. }  

text.xml文件:

  1. <!--test.xml-->  
  2. <?xml version="1.0" encoding="UTF-8"?><!-- 开始文档语法 -->  
  3. <persons>  
  4.         <person  id="1">  
  5.             <name>ssun</name>  
  6.             <age>19</age>  
  7.         </person>  
  8.         <person  id="2">  
  9.             <name>cobe</name>  
  10.             <age>24</age>  
  11.         </person>  
  12. </persons>  

单元测试TestService.java源码:

  1. package com.sinaapp.ssun.test;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8.   
  9. import android.test.AndroidTestCase;  
  10. import android.util.Log;  
  11.   
  12. import com.sinaapp.ssun.domain.Person;  
  13. import com.sinaapp.ssun.service.Service;  
  14.   
  15. public class TestService extends AndroidTestCase {  
  16.     private final String Tag = "Test";  
  17.       
  18.     public void testPersons() throws Exception{  
  19.         List<Person> persons = Service.getPersons(this.getClass().getClassLoader().getResourceAsStream("test.xml"));  
  20.         for(Person p : persons){  
  21.             Log.i(Tag, p.toString());  
  22.         }  
  23.     }  
  24.       
  25.     public void testSave() throws Exception{  
  26.         List<Person> persons = new ArrayList<Person>();  
  27.         persons.add(new Person("www",19,23));  
  28.         persons.add(new Person("hhh",19,3));  
  29.         persons.add(new Person("qqq",19,24));   
  30.         persons.add(new Person("ooo",19,25));  
  31.         File file = new File(this.getContext().getFilesDir(),"test2.xml");  
  32.         FileOutputStream out = new FileOutputStream(file);  
  33.         Service.save(persons, out);  
  34.     }  
  35.       
  36. }  
  37.     
原创粉丝点击