android 通过pull解析xml文件

原文链接:https://my.oschina.net/u/2529405/blog/663602 android 通过pull解析xml文件android 通过pull解析xml文件
  1 package com.example.xmldemo.test;
  2 
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.FileNotFoundException;
  6 import java.io.FileOutputStream;
  7 import java.io.IOException;
  8 import java.io.InputStreamReader;
  9 import java.util.ArrayList;
 10 import java.util.List;
 11 
 12 import org.xmlpull.v1.XmlPullParser;
 13 import org.xmlpull.v1.XmlSerializer;
 14 
 15 import com.example.xmldemo.Person;
 16 
 17 import android.os.Environment;
 18 import android.test.AndroidTestCase;
 19 import android.util.Log;
 20 import android.util.Xml;
 21 
 22 public class XmlTest extends AndroidTestCase
 23 {    
 24     public void test()
 25     {
 26         writeLocal();
 27         
 28         List<Person> personList = parseXml();
 29         for(Person person : personList)
 30         {
 31             Log.i("XmlTest", person.toString());
 32         }
 33     }
 34     /**
 35      * 写xml文件大本地
 36      */
 37     public void writeLocal()
 38     {
 39         List<Person> personList = getPersonList();
 40         //获得序列化对象
 41         XmlSerializer serializer = Xml.newSerializer();
 42         // 
 43         try
 44         {
 45             //创建文件流对象
 46             File file = new File("data/data/com.example.xmldemo", "persons.xml");
 47             FileOutputStream fos = new FileOutputStream(file);
 48             //设置文件的输出流
 49             serializer.setOutput(fos, "UTF-8");
 50             serializer.startDocument("utf-8", true); //<?xml version
 51             serializer.startTag(null, "persons"); //<persons>
 52             for(Person person : personList)
 53             {
 54                 serializer.startTag(null, "person"); //<person>
 55                 serializer.attribute(null, "id", String.valueOf(person.getId()));
 56                 serializer.startTag(null, "name"); //<name>
 57                 serializer.text(person.getName()); // kite1
 58                 serializer.endTag(null, "name"); //</name>
 59                 serializer.startTag(null, "age"); //<age>
 60                 serializer.text(String.valueOf(person.getAge()));
 61                 serializer.endTag(null, "age");//</age>
 62                 serializer.endTag(null, "person"); //<person>
 63             }
 64             serializer.endTag(null, "persons"); //</persons>
 65             serializer.endDocument();
 66             serializer.flush();
 67             fos.flush();
 68             fos.close();
 69         } catch (Exception e)
 70         {
 71             e.printStackTrace();
 72         }
 73         
 74     }
 75     /**
 76      * 获得personList
 77      * @return
 78      */
 79     public List<Person> getPersonList()
 80     {
 81         List<Person> personList = new ArrayList<Person>();
 82         for(int i = 0; i < 30; i++)
 83         {
 84             Person person = new Person(i, "kite" + 1,18 + i);
 85             personList.add(person);
 86         }
 87         return personList;
 88     }
 89     
 90     public List<Person> parseXml()
 91     {
 92     
 93         try
 94         {
 95             //创建文件流对象
 96             File file = new File("data/data/com.example.xmldemo", "persons.xml");
 97             FileInputStream fis = new FileInputStream(file);
 98             //获得pull 解析对象
 99             XmlPullParser parser = Xml.newPullParser();
100             // 指定解析的文件和编码格式
101             parser.setInput(fis,"utf-8");
102             int eventType = parser.getEventType(); //获得事件的类型
103             
104             //初始化集合
105             List<Person> personList = null;
106             Person person = null;
107             //如果不是最后的endDocument
108             while(eventType != XmlPullParser.END_DOCUMENT)
109             {
110                 String tagName = parser.getName();
111                 switch (eventType)
112                 {
113                 case XmlPullParser.START_TAG:
114                         //如果是最开始的persons 根节点初始化
115                         if("persons".equals(tagName))
116                         {
117                             personList = new ArrayList<Person>();
118                         } else if("person".equals(tagName))
119                         {
120                             //初始化person对象
121                             person = new Person();
122                             
123                             //获得id
124                             String id =  parser.getAttributeValue(null, "id");
125                             person.setId(Integer.parseInt(id));
126                         } else if("name".equals(tagName))
127                         {
128                             person.setName(parser.nextText());
129                         } else if("age".equals(tagName))
130                         {
131                             person.setAge(Integer.parseInt(parser.nextText()));
132                         }
133                     break;
134                 case XmlPullParser.END_TAG:
135                     //如果是结束标记
136                     if("person".equals(tagName))
137                     {
138                         // 需要把上面设置好值的person对象添加到集合中
139                         personList.add(person);
140                     } 
141                     break;
142                 default:    
143                     break;
144                 }
145                 //获得下一个事件
146                 eventType = parser.next();
147             }
148             return personList;
149         } catch (Exception e)
150         {
151             // TODO Auto-generated catch block
152             e.printStackTrace();
153         }
154         return null;
155     }
156     
157 }
View Code

 

转载于:https://my.oschina.net/u/2529405/blog/663602

上一篇:【JDK8特性】Stream接口详解


下一篇:java封神之路-stream流-进阶