Android解析XML文件
前言
XML
用途
- 丰富文件——自定文件描述并使其更丰富
- 元数据——描述其它文件或网络资讯
- 配置文档——描述软件设置的参数
结构
XmlPullParser
xml示例文件
<?xml version="1.0" encoding="UTF-8"?> <colleagues> <colleague id="1"> <name>耗子</name> <age>24</age> <sex>boy</sex> </colleague> <colleague id="2"> <name>璐璐</name> <age>28</age> <sex>girl</sex> </colleague> <colleague id="3"> <name>陈善</name> <age>26</age> <sex>boy</sex> </colleague> </colleagues>
XmlPullParser解析器
package com.example.shakedemo; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; 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.R.xml; import android.util.Log; import android.util.Xml; public class XmlPullParserHelper { public static List<Colleague> getColleagues(String xmlFilePath) { List<Colleague> colleagues = new ArrayList<Colleague>(); FileReader xmlReader = null; try { xmlReader = new FileReader(new File(xmlFilePath)); } catch (FileNotFoundException e) { Log.e("wzy", "Couldn't find xml file " + xmlFilePath); return colleagues; } try { // 方式1: 使用 Android 提供的android.util.Xml 类获取 parser 对象 XmlPullParser parser = Xml.newPullParser(); // 方式2: 使用工厂类 XmlPullParserFactory // XmlPullParserFactory pullFactory = // XmlPullParserFactory.newInstance(); // XmlPullParser parser = pullFactory.newPullParser(); // 设置文件输入流 parser.setInput(xmlReader); // 得到当前事件类型 int eventType = parser.getEventType(); Colleague colleague = null; while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: /** * 通过getName判断读到哪个标签, 然后通过nextText获取文本节点值, * 或者通过getAttributeValue(i)获取属性节点值 */ String name = parser.getName(); if ("colleague".equals(name)) { colleague = new Colleague(); colleague.setId(Integer.parseInt(parser.getAttributeValue(null, "id"))); } else if ("name".equals(name)) { if (colleague != null) { colleague.setName(parser.nextText()); } } else if ("age".equals(name)) { if (colleague != null) { colleague.setAge(Integer.parseInt(parser.nextText())); } } else if ("sex".equals(name)) { if (colleague != null) { colleague.setSex(parser.nextText()); } } break; case XmlPullParser.END_TAG: if ("colleague".equals(parser.getName()) && colleague != null) { colleagues.add(colleague); colleague = null; } break; } eventType = parser.next(); } xmlReader.close(); } catch (XmlPullParserException e) { // Do nothing } catch (IOException e) { // Do nothing } return colleagues; } }其中,colleague类的定义比较简单,代码如下:
package com.example.shakedemo; public class Colleague { private int id; private int age; private String name; private String sex; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "ID is " + id + ", Name is " + name + ", Sex is " + sex; } }有时间,我会考虑增加其他XML文件的解析方法!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。