有个升级项目需要使用到xml文件,由于XmlPullParser最简单所以选择它也完全满足当前的需求应用。
下面就简要的说明一下具体的用法。
1、基本原理:
常用的有:
XmlPullParser.END_DOCUMENT
XmlPullParser.START_DOCUMENT
XmlPullParser.START_TAG
XmlPullParser.END_TAG
XmlPullParser.TEXT
分别代表着XML文档的结束,开始,标签的开始,标签的结束,内容
按照以上的格式依次进行解析即可。
2、介绍了三种不同的xml格式及解析方法
新建一个新的activiy文件,其实就是程序启动发源地而已。
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());
}
}
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;
}
};
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 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>
/* 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());
}
}
/* 直接使用内存测试 */
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 ;
}
/* 另类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 ;
}