pull解析xml(android)

   1. package com.cn.service;   
   2.   
   3. import java.io.IOException;   
   4. import java.io.InputStream;   
   5. import java.io.OutputStream;   
   6. import java.util.ArrayList;   
   7. import java.util.List;   
   8.   
   9. import org.xmlpull.v1.XmlPullParser;   
  10. import org.xmlpull.v1.XmlSerializer;   
  11.   
  12. import android.util.Xml;   
  13.   
  14. import com.cn.domain.Persion;   
  15. /****  
  16.  * 采用Pull解析XML  
  17.  *   
  18.  * ****/  
  19. public class PULLPersionService {   
  20.   
  21.     public static List<Persion> getPersino(InputStream is)throws Exception{   
  22.         List<Persion> list = null;   
  23.         Persion persion = null;   
  24.         XmlPullParser parser = Xml.newPullParser();   
  25.         parser.setInput(is, "UTF-8");   
  26.         int event = parser.getEventType();   
  27.         while(event != XmlPullParser.END_DOCUMENT){   
  28.             switch (event){   
  29.             case XmlPullParser.START_DOCUMENT:   
  30.                 list =  new ArrayList<Persion>();   
  31.                 break;   
  32.             case XmlPullParser.START_TAG:   
  33.                 if("person".equals(parser.getName())){//得到指针所指向的节点名称   
  34.                     int id = Integer.parseInt(parser.getAttributeValue(0));   
  35.                     persion = new Persion();   
  36.                     persion.setId(id);   
  37.                 }   
  38.                 if(persion != null){   
  39.                     if("name".equals(parser.getName())){   
  40.                         String name = parser.nextText();   
  41.                         persion.setName(name);   
  42.                     }else if("age".equals(parser.getName())){   
  43.                         String age = parser.nextText();   
  44.                         persion.setName(age);   
  45.                     }   
  46.                 }   
  47.                 break;   
  48.             case XmlPullParser.END_TAG:   
  49.                 if("person".equals(parser.getName())){   
  50.                     list.add(persion);   
  51.                     persion = null;   
  52.                 }   
  53.                 break;   
  54.             default:   
  55.                 break;   
  56.             }   
  57.         }   
  58.         event = parser.next();   
  59.         return list;   
  60.     }   
  61.        
  62.     public static void save(List<Persion> list,OutputStream os) throws IllegalArgumentException, IllegalStateException, IOException{   
  63.         XmlSerializer serializer = Xml.newSerializer();   
  64.         serializer.setOutput(os, "UTF-8");   
  65.         serializer.startDocument("UTF-8", true);   
  66.         serializer.startTag(null, "persons");   
  67.         for(Persion persion : list){   
  68.             serializer.startTag(null, "person");   
  69.             serializer.attribute(null, "id", String.valueOf(persion.getId()));   
  70.                 serializer.startTag(null, "name");   
  71.                 serializer.text(persion.getName());   
  72.                 serializer.endTag(null, "name");   
  73.                    
  74.                 serializer.startTag(null, "age");   
  75.                 serializer.text(String.valueOf(persion.getAge()));   
  76.                 serializer.endTag(null, "age");   
  77.             serializer.endTag(null, "person");   
  78.         }   
  79.         serializer.endTag(null, "persons");   
  80.         serializer.endDocument();   
  81.         os.flush();   
  82.         os.close();   
  83.     }   
  84. }  

 
posted @ 2011-08-01 16:52  上善¤若水  阅读(120)  评论(0编辑  收藏  举报