简单介绍:
在Android应用开发中Pull方式解析XML是最被推荐的一种,Android系统内置了Pull解析器用来解析XML文件,它是采用事件驱动来完成XML解析的,只需要处理开始和结束事件,通常使用switch语句根据事件的类型匹配不同的处理方式。
最常使用的事件类型:
1. START_DOCUMENT:开始解析(只执行一次)
2. START_TAG:开始元素
3. TEXT:解析文本
4. END_TAG:结束元素
5. END_DOCUMENT:结束解析(只执行一次)
最常使用的方法:
1. getEventType():触发并获取第一个事件类型(START_DOCUMENT)
2. getName():获取元素名称
3. nextText():获取当前元素的值
4. next():触发下一个事件
5. getAttributeValue():获取当前元素的属性值(参数为索引,还有一个重载方法,是通过指定命名空间获取的)
用代码实现一下:
在assets目录中创建一个XML文件
<?xml version="1.0" encoding="utf-8"?>
<persons>
<person>
<id>1</id>
<name>zhangsan</name>
<age>18</age>
</person>
<person>
<id>2</id>
<name>lisi</name>
<age>19</age>
</person>
</persons>
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivityInfo";
private Button btnPull;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/**
* 初始化UI
*/
private void initView() {
btnPull = (Button) findViewById(R.id.btn_pull);
btnPull.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InputStream inputStream = getXmlData();
try {
parseXMLWithPull(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
/**
* 从assets目录中获取xml数据
*
* @return xml数据
*/
public InputStream getXmlData() {
//获取assets管理者
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
//打开文件获取输入流
//inputStream输入流不能在此关闭,因为它还要作为其它方法的参数
inputStream = assetManager.open("persons.xml");
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
}
/**
* 使用Pull方式解析xml
*
* @param xmlData xml数据
*/
private void parseXMLWithPull(InputStream xmlData) throws IOException {
try {
//创建一个xml解析的工厂
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
//获取xml解析类的实例
XmlPullParser parser = factory.newPullParser();
//设置需要解析数据
parser.setInput(xmlData, "UTF-8");
//获取解析事件
int eventType = parser.getEventType();
switch (parser.getEventType()) {
case 0:
Log.i(TAG, "START_DOCUMENT");
break;
case 1:
Log.i(TAG, "END_DOCUMENT");
break;
case 2:
Log.i(TAG, "START_TAG");
break;
case 3:
Log.i(TAG, "END_TAG");
break;
case 4:
Log.i(TAG, "TEXT");
break;
}
String id = "";
String name = "";
String age = "";
while (eventType != XmlPullParser.END_DOCUMENT) {
String nodeName = parser.getName();
Log.i(TAG, "nodeName = " + nodeName);
switch (eventType) {
//开始解析某个结点
case XmlPullParser.START_TAG:
if ("id".equals(nodeName)) {
id = parser.nextText();
} else if ("name".equals(nodeName)) {
name = parser.nextText();
} else if ("age".equals(nodeName)) {
age = parser.nextText();
}
break;
//完成解析某个结点
case XmlPullParser.END_TAG:
if ("person".equals(nodeName)) {
Log.i(TAG, "id = " + id);
Log.i(TAG, "name = " + name);
Log.i(TAG, "age = " + age);
}
break;
}
//获取下一个解析事件
eventType = parser.next();
switch (parser.getEventType()) {
case 1:
Log.i(TAG, "END_DOCUMENT");
break;
case 2:
Log.i(TAG, "START_TAG");
break;
case 3:
Log.i(TAG, "END_TAG");
break;
case 4:
Log.i(TAG, "TEXT");
break;
}
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//最后在关闭输入流
xmlData.close();
}
}
打印信息:
03-20 16:09:11.047 7554-7554/? I/MainActivityInfo: START_DOCUMENT
03-20 16:09:11.047 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: nodeName = persons
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: nodeName = person
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: nodeName = id
03-20 16:09:11.049 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.049 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.049 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.049 7554-7554/? I/MainActivityInfo: nodeName = name
03-20 16:09:11.050 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.050 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.050 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: nodeName = age
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: END_TAG
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: nodeName = person
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: id = 1
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: name = zhangsan
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: age = 18
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: nodeName = person
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: nodeName = id
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: nodeName = name
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: nodeName = age
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: END_TAG
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: nodeName = person
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: id = 2
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: name = lisi
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: age = 19
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: END_TAG
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: nodeName = persons
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: END_DOCUMENT