Android中常见的xml解析方式有sax、dom和pull,下面我们就看一个小巧轻便,解析方便,速度很快的pull方式的实例。
先建一个xml吧:mission.xml
<?xml version="1.0" encoding="UTF-8" ?><region id="region_1" width="100" height="800"><element id="1" type="20">Chinese</element><element id="2" type="20">English</element></region>

建region的实体: Region.java
package com.hebaijun.xmlparser;import java.util.List;public class Region {private String id;private int width;private int height;private List<Element> elements;public String getId() {return id;}public void setId(String id) {this.id = id;}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public List<Element> getElements() {return elements;}public void setElements(List<Element> elements) {this.elements = elements;}}

Element的实体:Element.java
package com.hebaijun.xmlparser;public class Element {private String id;private String type;private String value;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getType() {return type;}public void setType(String type) {this.type = type;}public String getValue() {return value;}public void setValue(String value) {this.value = value;}}

最后是实例代码:
package com.hebaijun.xmlparser;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import org.xmlpull.v1.XmlPullParserFactory;import android.app.Activity;import android.os.Bundle;import android.util.Log;public class XmlParserActivity extends Activity {    /** Called when the activity is first created. */Region region;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //取xml的路径        String xmlPath = getExternalStoragePath() + "/mission.xml";        try {region = parser(xmlPath);} catch (XmlPullParserException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}        //打印一些结果        Log.v("region_id", region.getId());        for (Element element : region.getElements()) {        Log.v("element_value", element.getValue());}    }        //获取sdcard路径  public static String getExternalStoragePath(){  //获取状态  String state = android.os.Environment.getExternalStorageState();  //判断是否可读  if (android.os.Environment.MEDIA_MOUNTED.equals(state)) {  if (android.os.Environment.getExternalStorageDirectory().canRead()) {  return android.os.Environment.getExternalStorageDirectory().getPath();  }  }  return null;  }    public Region parser(String path) throws XmlPullParserException, IOException{File xmlFile = new File(path);Element xmlElement = null;List<Element> xmlElements = null;Region xmlRegion = null;if (xmlFile.exists()) {InputStream slideInputStream = new FileInputStream(path);XmlPullParserFactory factory = XmlPullParserFactory.newInstance();         factory.setNamespaceAware(true);         XmlPullParser xpp = factory.newPullParser();        xpp.setInput(slideInputStream, "UTF-8");        int eventType = xpp.getEventType();         while (eventType != XmlPullParser.END_DOCUMENT) {         if(eventType == XmlPullParser.START_DOCUMENT) {                 } else if(eventType == XmlPullParser.START_TAG) {         String startName = xpp.getName();        if (startName.equalsIgnoreCase("region")) {        xmlRegion = new Region();        xmlElements = new ArrayList<Element>();        int count = xpp.getAttributeCount();        for (int i = 0; i < count; i++) {        String name = xpp.getAttributeName(i);        String value = xpp.getAttributeValue(i);        if (name.equalsIgnoreCase("id")) {        xmlRegion.setId(value);        } else if (name.equalsIgnoreCase("width")) {        xmlRegion.setWidth(Integer.parseInt(value));        } else if (name.equalsIgnoreCase("height")) {        xmlRegion.setHeight(Integer.parseInt(value));        }        }} else if (startName.equalsIgnoreCase("element")) {xmlElement = new Element();int count = xpp.getAttributeCount();        for (int i = 0; i < count; i++) {        String name = xpp.getAttributeName(i);        String value = xpp.getAttributeValue(i);        if (name.equalsIgnoreCase("id")) {        xmlElement.setId(value);        } else if (name.equalsIgnoreCase("type")) {        xmlElement.setType(value);        }        }        //元素的text值        xpp.next();        xmlElement.setValue(xpp.getText());}         } else if(eventType == XmlPullParser.END_TAG) {         String endName = xpp.getName();        if (endName.equalsIgnoreCase("region")) {        if (xmlElements != null) {xmlRegion.setElements(xmlElements);}} else if (endName.equalsIgnoreCase("element")) {xmlElements.add(xmlElement);}        }        //下一个元素        eventType = xpp.next();         }                 //关闭输入流        if (slideInputStream != null) {        slideInputStream.close();        slideInputStream = null;}}return xmlRegion;}}

更多相关文章

  1. Netty多语言(Java、Android 、C#、WebSocket)通信实例Demo (一)概述
  2. Sensor传感器源码的阅读与应用开发简单实例
  3. Android studio插件GsonFormat,返回json快速创建实体对象
  4. Android中SharedPreference实例
  5. Android Google Map实例 - 不同的图标标注在同一图层(Android ma
  6. [Android实例] Android实现开机自动运行程序

随机推荐

  1. Android 自定义搜索Searchable
  2. Android快速上手
  3. Android Selector 与 Shape 基本用法
  4. Android新增AppCompatTextView自适应字体
  5. Android:控件的隐藏显示失效了
  6. cc
  7. 近百android程序源码贡献
  8. Android(安卓)xml 中@和问号?等解释
  9. ListView 常用属性
  10. Android 属性动画(Property Animation) 完