Android中常见的xml解析方式有sax、dom和pull,下面我们就看一个小巧轻便,解析方便,速度很快的pull方式的实例。
建region的实体: Region.java
Element的实体:Element.java
最后是实例代码:
先建一个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;
- }
- }
PS: XML中 notice貌似是关键字,如果有的话,会导致失败 提示attr value delimiter missing!