PULL解析XML

Android中常见的xml解析方式有sax、dom和pull,下面我们就看一个小巧轻便,解析方便,速度很快的pull方式的实例。

先建一个xml吧:mission.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <region id="region_1" width="100" height="800">  
  3.     <element id="1" type="20">Chinese</element>  
  4.     <element id="2" type="20">English</element>  
  5. </region>  

建region的实体: Region.java
  1. package com.hebaijun.xmlparser;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class Region {  
  6.     private String id;  
  7.     private int width;  
  8.     private int height;  
  9.     private List<Element> elements;  
  10.     public String getId() {  
  11.         return id;  
  12.     }  
  13.     public void setId(String id) {  
  14.         this.id = id;  
  15.     }  
  16.     public int getWidth() {  
  17.         return width;  
  18.     }  
  19.     public void setWidth(int width) {  
  20.         this.width = width;  
  21.     }  
  22.     public int getHeight() {  
  23.         return height;  
  24.     }  
  25.     public void setHeight(int height) {  
  26.         this.height = height;  
  27.     }  
  28.     public List<Element> getElements() {  
  29.         return elements;  
  30.     }  
  31.     public void setElements(List<Element> elements) {  
  32.         this.elements = elements;  
  33.     }  
  34.   
  35. }  

Element的实体:Element.java
  1. package com.hebaijun.xmlparser;  
  2.   
  3. public class Element {  
  4.     private String id;  
  5.     private String type;  
  6.     private String value;  
  7.     public String getId() {  
  8.         return id;  
  9.     }  
  10.     public void setId(String id) {  
  11.         this.id = id;  
  12.     }  
  13.     public String getType() {  
  14.         return type;  
  15.     }  
  16.     public void setType(String type) {  
  17.         this.type = type;  
  18.     }  
  19.     public String getValue() {  
  20.         return value;  
  21.     }  
  22.     public void setValue(String value) {  
  23.         this.value = value;  
  24.     }  
  25. }  

最后是实例代码:
  1. package com.hebaijun.xmlparser;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9.   
  10. import org.xmlpull.v1.XmlPullParser;  
  11. import org.xmlpull.v1.XmlPullParserException;  
  12. import org.xmlpull.v1.XmlPullParserFactory;  
  13.   
  14. import android.app.Activity;  
  15. import android.os.Bundle;  
  16. import android.util.Log;  
  17.   
  18. public class XmlParserActivity extends Activity {  
  19.     /** Called when the activity is first created. */  
  20.     Region region;  
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.         //取xml的路径  
  26.         String xmlPath = getExternalStoragePath() + "/mission.xml";  
  27.         try {  
  28.             region = parser(xmlPath);  
  29.         } catch (XmlPullParserException e) {  
  30.             e.printStackTrace();  
  31.         } catch (IOException e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.         //打印一些结果  
  35.         Log.v("region_id", region.getId());  
  36.         for (Element element : region.getElements()) {  
  37.             Log.v("element_value", element.getValue());  
  38.         }  
  39.     }  
  40.       
  41.     //获取sdcard路径  
  42.     public static String getExternalStoragePath(){  
  43.         //获取状态  
  44.         String state = android.os.Environment.getExternalStorageState();  
  45.         //判断是否可读  
  46.         if (android.os.Environment.MEDIA_MOUNTED.equals(state)) {  
  47.             if (android.os.Environment.getExternalStorageDirectory().canRead()) {  
  48.                 return android.os.Environment.getExternalStorageDirectory().getPath();  
  49.             }  
  50.         }  
  51.         return null;  
  52.     }  
  53.       
  54.     public Region parser(String path) throws XmlPullParserException, IOException{  
  55.         File xmlFile = new File(path);  
  56.         Element xmlElement = null;  
  57.         List<Element> xmlElements = null;  
  58.         Region xmlRegion = null;  
  59.         if (xmlFile.exists()) {  
  60.             InputStream slideInputStream = new FileInputStream(path);  
  61.             XmlPullParserFactory factory = XmlPullParserFactory.newInstance();   
  62.             factory.setNamespaceAware(true);   
  63.             XmlPullParser xpp = factory.newPullParser();  
  64.             xpp.setInput(slideInputStream, "UTF-8");  
  65.             int eventType = xpp.getEventType();   
  66.             while (eventType != XmlPullParser.END_DOCUMENT) {   
  67.                 if(eventType == XmlPullParser.START_DOCUMENT) {   
  68.                       
  69.                 } else if(eventType == XmlPullParser.START_TAG) {   
  70.                     String startName = xpp.getName();  
  71.                     if (startName.equalsIgnoreCase("region")) {  
  72.                         xmlRegion = new Region();  
  73.                         xmlElements = new ArrayList<Element>();  
  74.                         int count = xpp.getAttributeCount();  
  75.                         for (int i = 0; i < count; i++) {  
  76.                             String name = xpp.getAttributeName(i);  
  77.                             String value = xpp.getAttributeValue(i);  
  78.                             if (name.equalsIgnoreCase("id")) {  
  79.                                 xmlRegion.setId(value);  
  80.                             } else if (name.equalsIgnoreCase("width")) {  
  81.                                 xmlRegion.setWidth(Integer.parseInt(value));  
  82.                             } else if (name.equalsIgnoreCase("height")) {  
  83.                                 xmlRegion.setHeight(Integer.parseInt(value));  
  84.                             }  
  85.                         }  
  86.                     } else if (startName.equalsIgnoreCase("element")) {  
  87.                         xmlElement = new Element();  
  88.                         int count = xpp.getAttributeCount();  
  89.                         for (int i = 0; i < count; i++) {  
  90.                             String name = xpp.getAttributeName(i);  
  91.                             String value = xpp.getAttributeValue(i);  
  92.                             if (name.equalsIgnoreCase("id")) {  
  93.                                 xmlElement.setId(value);  
  94.                             } else if (name.equalsIgnoreCase("type")) {  
  95.                                 xmlElement.setType(value);  
  96.                             }  
  97.                         }  
  98.                         //元素的text值  
  99.                         xpp.next();  
  100.                         xmlElement.setValue(xpp.getText());  
  101.                     }   
  102.                 } else if(eventType == XmlPullParser.END_TAG) {   
  103.                     String endName = xpp.getName();  
  104.                     if (endName.equalsIgnoreCase("region")) {  
  105.                         if (xmlElements != null) {  
  106.                             xmlRegion.setElements(xmlElements);  
  107.                         }  
  108.                     } else if (endName.equalsIgnoreCase("element")) {  
  109.                         xmlElements.add(xmlElement);  
  110.                     }  
  111.                 }  
  112.                 //下一个元素  
  113.                 eventType = xpp.next();   
  114.             }   
  115.               
  116.             //关闭输入流  
  117.             if (slideInputStream != null) {  
  118.                 slideInputStream.close();  
  119.                 slideInputStream = null;  
  120.             }  
  121.         }  
  122.         return xmlRegion;  
  123.     }  



PS:  XML中 notice貌似是关键字,如果有的话,会导致失败  提示attr value delimiter missing!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值