安卓中使用XmlPullParser解析xml文件
有个升级项目需要使用到xml文件,由于XmlPullParser最简单所以选择它也完全满足当前的需求应用。
下面就简要的说明一下具体的用法。
1、基本原理:
常用的有:
XmlPullParser.END_DOCUMENT
XmlPullParser.START_DOCUMENT
XmlPullParser.START_TAG
XmlPullParser.END_TAG
XmlPullParser.TEXT
分别代表着XML文档的结束,开始,标签的开始,标签的结束,内容
按照以上的格式依次进行解析即可。
2、介绍了三种不同的xml格式及解析方法
新建一个新的activiy文件,其实就是程序启动发源地而已。
<span style="font-size: 18px;">public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xmltest xl = new xmltest();
xl.test(getResources());
}
}</span>
为了便于xml解析内容的存取,新建了一个Person.java类,内容如下:
package com.test.xmlactivity;
class Person{
private int id = -1;
private int age = 0;
private String name = null;
private String title = null;
public Person(){
//TODO
}
public Person(int _id,int _age,String _name,String _title){
this.id = _id ;
this.age = _id;
this.name = _name;
this.title = _title;
}
public void setID(int id){
this.id = id;
}
public int getID(){
return this.id;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setTitle(String title){
this.title = title;
}
public String getTitle(){
return this.title;
}
};
为方便其它程序简洁使用xml的读取及解析,封装了一个xmlReader.java类,内容如下:
package com.test.xmlactivity;
import java.io.IOException;
import java.io.StringReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.util.Log;
class XmlReader{
private final static String TAG = "XmlReader";
private XmlPullParser xmlparser = null;
public static final String charset = "utf-8";
public XmlReader(){
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
xmlparser = factory.newPullParser();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG,"create xml reader failed");
}
}
public void setXML(String xml) throws IOException{
try {
xmlparser.setInput(new StringReader(xml));
} catch (Exception e) {
e.printStackTrace();
}
}
public int getNextEvent() throws IOException {
try {
return xmlparser.next();
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
public String getNextText() throws IOException{
try {
return xmlparser.nextText();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public int getEventType() throws IOException {
try {
return xmlparser.getEventType();
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
public String getName() {
return xmlparser.getName();
}
public String getText() {
return xmlparser.getText();
}
public int attributeCount() {
return xmlparser.getAttributeCount();
}
public String getAttributeName(int i) {
return xmlparser.getAttributeName(i);
}
public String getAttributeValue(String name) {
String v = xmlparser.getAttributeValue(null, name);
return v;
}
public int getDepth() {
return xmlparser.getDepth();
}
};
需要读取的xml资源文件放在 res\xm;\person.xml,其具体内容如下:
<?xml version="1.0" encoding="utf-8"?>
<White-collars>
<employee id="1">
<name>tom</name>
<age>25</age>
<title>engineer</title>
</employee>
<employee id="2">
<name>kite</name>
<age>21</age>
<title>tester</title>
</employee>
<employee id="3">
<name>rose</name>
<age>28</age>
<title>manager</title>
</employee>
</White-collars>
a、从资源文件中读取
需要使用系统资源,必须有Context对象,而Activity可以持有,所以需要在测试前将其资源句柄传入
xl.test(getResources());
而在具体的类中记录下来即可使用
private XmlReader XR = null;
private ArrayList mperlist = new ArrayList();
private Resources mRes = null;
public void test(Resources _res){
this.mRes = _res;
xml_test_main();
}
下面说明一下具体的xml内容解析:
/* 1、使用文件测试 */
private void xml_file_test(){
int eventType = -1;
Person ps = null;
XmlResourceParser xrp = mRes.getXml(R.xml.personal);
try {
eventType = xrp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.END_DOCUMENT:
break;
case XmlPullParser.START_TAG: {
String tagName = xrp.getName();
if (tagName != null && tagName.equals("employee")) {
ps = new Person();
int id = Integer.parseInt(xrp.getAttributeValue(null, "id"));
ps.setID(id);
}
if (tagName != null && tagName.equals("name")) {
String name;
try {
name = xrp.nextText();;
ps.setName(name);
} catch (IOException e) {
e.printStackTrace();
}
}
if (tagName != null && tagName.equals("title")) {
String title;
try {
title = xrp.nextText();
ps.setTitle(title);
} catch (IOException e) {
e.printStackTrace();
}
}
if (tagName != null && tagName.equals("age")) {
String age;
try {
age = xrp.nextText();
ps.setAge(Integer.parseInt(age));
} catch (IOException e) {
e.printStackTrace();
}
}
}
break;
case XmlPullParser.END_TAG: {
if (xrp.getName().equals("employee")) {
mperlist.add(ps);
}
}
break;
case XmlPullParser.TEXT:
break;
default:
break;
}
eventType = xrp.next();
}
} catch (Exception e) {
// TODO: handle exception
}
for(int i=0;i<mperlist.size();i++){
Log.i(TAG,"person info ====");
Log.i(TAG,"ID = " + mperlist.get(i).getID());
Log.i(TAG,"name = "+ mperlist.get(i).getName());
Log.i(TAG,"age = " + mperlist.get(i).getAge());
Log.i(TAG,"title = " + mperlist.get(i).getTitle());
}
}
相应的打印内容:
10-02 10:47:50.725: I/xmltest(12364): person info ====
10-02 10:47:51.094: I/xmltest(12364): ID = 1
10-02 10:47:51.382: I/xmltest(12364): name = tom
10-02 10:47:51.647: I/xmltest(12364): age = 25
10-02 10:47:51.986: I/xmltest(12364): title = engineer
10-02 10:48:03.057: I/xmltest(12364): person info ====
10-02 10:48:03.057: I/xmltest(12364): ID = 2
10-02 10:48:03.057: I/xmltest(12364): name = kite
10-02 10:48:03.057: I/xmltest(12364): age = 21
10-02 10:48:03.057: I/xmltest(12364): title = tester
10-02 10:48:05.038: I/xmltest(12364): person info ====
10-02 10:48:05.038: I/xmltest(12364): ID = 3
10-02 10:48:05.038: I/xmltest(12364): name = rose
10-02 10:48:05.038: I/xmltest(12364): age = 28
10-02 10:48:05.038: I/xmltest(12364): title = manager
b、直接从内存中读取
/* 直接使用内存测试 */
private void xml_memory_test(){
String xml_test = "<foo>Hello World!</foo>";
try {
XR.setXML(xml_test);
int eventType = XR.getEventType();
Log.i(TAG,"eventType: " + eventType);
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {
Log.i(TAG,"Start document");
} else if (eventType == XmlPullParser.START_TAG) {
Log.i(TAG,"Start tag " + XR.getName());
} else if (eventType == XmlPullParser.END_TAG) {
Log.i(TAG,"End tag " + XR.getName());
} else if (eventType == XmlPullParser.TEXT) {
Log.i(TAG,"Text " + XR.getText());
}
eventType = XR.getNextEvent();
Log.i(TAG,"eventType: " + eventType);
}
Log.i(TAG,"End document");
} catch (Exception e) {
e.printStackTrace();
}
return ;
}
输出内容:
10-02 10:48:07.975: I/xmltest(12364): eventType: 0
10-02 10:48:07.975: I/xmltest(12364): Start document
10-02 10:48:07.983: I/xmltest(12364): eventType: 2
10-02 10:48:07.983: I/xmltest(12364): Start tag foo
10-02 10:48:07.991: I/xmltest(12364): eventType: 4
10-02 10:48:07.999: I/xmltest(12364): Text Hello World!
10-02 10:48:07.999: I/xmltest(12364): eventType: 3
10-02 10:48:07.999: I/xmltest(12364): End tag foo
10-02 10:48:08.006: I/xmltest(12364): eventType: 1
10-02 10:48:08.006: I/xmltest(12364): End document
c、另外的xml格式用法
/* 另类xml文件写法 */
private void xml_misc_test(){
String xml_test = "<foo hi=\\"Hello World!\\"/>";
try {
XR.setXML(xml_test);
int eventType = XR.getEventType();
Log.i(TAG,"eventType: " + eventType);
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {
Log.i(TAG,"Start document");
} else if (eventType == XmlPullParser.START_TAG) {
Log.i(TAG,"Start tag " + XR.getName());
String name = XR.getAttributeName(0);
String value = XR.getAttributeValue(name);
Log.i(TAG,"name:" + name + " value:" + value);
} else if (eventType == XmlPullParser.END_TAG) {
Log.i(TAG,"End tag " + XR.getName());
} else if (eventType == XmlPullParser.TEXT) {
Log.i(TAG,"Text " + XR.getText());
}
eventType = XR.getNextEvent();
Log.i(TAG,"eventType: " + eventType);
}
Log.i(TAG,"End document");
} catch (Exception e) {
e.printStackTrace();
}
return ;
}
输出内容:
10-02 10:48:08.014: I/xmltest(12364): eventType: 0
10-02 10:48:08.014: I/xmltest(12364): Start document
10-02 10:48:08.014: I/xmltest(12364): eventType: 2
10-02 10:48:08.014: I/xmltest(12364): Start tag foo
10-02 10:48:08.014: I/xmltest(12364): name:hi value:Hello World!
10-02 10:48:08.014: I/xmltest(12364): eventType: 3
10-02 10:48:08.014: I/xmltest(12364): End tag foo
10-02 10:48:08.014: I/xmltest(12364): eventType: 1
10-02 10:48:08.014: I/xmltest(12364): End document