1、pull解析的介绍
除了可以使用 SAX和DOM解析XML文件,大家也可以使用Android内置的Pull解析器解析XML文件。 Pull解析器的运行方式与 SAX 解析器相似。它提供了类似的事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素并触发相应事件。事件将作为数值代码被发送,因此可以使用一个switch对感兴趣的事件进行处理。当元素开始解析时,调用parser.nextText()方法可以获取下一个Text类型元素的值。
2、pull需要解析的文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- 元素节点 --> <!-- 属性节点 --><!-- 文本节点 -->
<china dn="day">
<city
cityName="南京"
pyName="jiangsu"
quName="江苏"
state1="1"
state2="1"
stateDetailed="多云"
tem1="24"
tem2="19"
windState="西北风3~4级" />
<city
cityName="北京"
pyName="beijing"
quName="北京"
state1="1"
state2="1"
stateDetailed="多云"
tem1="29"
tem2="19"
windState="西北风3~6级" />
<!-- 下面的格式与上面的在实际开发中不可能同时存在,选择其一即可 -->
<city>
<cityName>河南</cityName>
<pyName>henan</pyName>
<quName>河南</quName>
<state1>1</state1>
<state2>1</state2>
<stateDetailed>多云转晴</stateDetailed>
<tem1>38</tem1>
<tem2>-1</tem2>
<windState>东南风3~4级</windState>
</city>
</china>
3、解析的过程
Xml.newPullParser()--->setInput-->getEventType()--->while(type!=XmlPullParser.END_DOCUMENT)-->case type --> parser.getName()去判断---->获得的信息添加进对象--->type=parser.next();指针下移。
4、举例说明,请仔细阅读
package www.csdn.net.xml;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import www.csdn.net.domain.City;
import android.util.Log;
import android.util.Xml;
public class PullXml {
private static final String TAG="PullXml";
public List<City> pullXml(){
List<City> entities=null;
City currentCity=null;
/*XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
factory.newPullParser();*/
//直接创建出XmlPullPraser()解析器对象
XmlPullParser xmlPullParser=Xml.newPullParser();
try {
//设置解析的文件输入流 并且指定输入流操作的编码方式
xmlPullParser.setInput(getClass().getClassLoader().getResourceAsStream("china.xml"), "UTF-8");
//获取解析文件时返回的EventType
int eventType=xmlPullParser.getEventType();
while(eventType!=XmlPullParser.END_DOCUMENT){
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
Log.i(TAG, "------startdocument-------");
entities=new ArrayList<City>();
break;
/*case XmlPullParser.END_DOCUMENT:
Log.i(TAG, "------enddocument-------");
break;*/
case XmlPullParser.START_TAG:
//获取标签名
String name=xmlPullParser.getName();
if(name.equals("city")){
//声明当前city对象
currentCity=new City();
int count=xmlPullParser.getAttributeCount();
//System.out.println("个数:"+count);
if(count>0){//只有一种格式存在时,不需要判断count
//根据属性赋值
currentCity.setCityName(xmlPullParser.getAttributeValue(null, "cityName"));
currentCity.setPyName(xmlPullParser.getAttributeValue(null, "pyName"));
currentCity.setQuName(xmlPullParser.getAttributeValue(null, "quName"));
currentCity.setState1(xmlPullParser.getAttributeValue(null, "state1"));
currentCity.setState2(xmlPullParser.getAttributeValue(null, "state2"));
currentCity.setStateDetailed(xmlPullParser.getAttributeValue(null, "stateDetailed"));
currentCity.setTem1(xmlPullParser.getAttributeValue(null, "tem1"));
currentCity.setTem2(xmlPullParser.getAttributeValue(null, "tem2"));
currentCity.setWindState(xmlPullParser.getAttributeValue(null, "windState"));
}
}else if(currentCity!=null){//第二种格式的判断方法
if(name.equalsIgnoreCase("cityName")){
currentCity.setCityName(xmlPullParser.nextText());
}else if(name.equalsIgnoreCase("pyName")){
currentCity.setPyName(xmlPullParser.nextText());
}else if(name.equalsIgnoreCase("quName")){
currentCity.setQuName(xmlPullParser.nextText());
}else if(name.equalsIgnoreCase("state1")){
currentCity.setState1(xmlPullParser.nextText());
}else if(name.equalsIgnoreCase("state2")){
currentCity.setState2(xmlPullParser.nextText());
}else if(name.equalsIgnoreCase("stateDetailed")){
currentCity.setStateDetailed(xmlPullParser.nextText());
}else if(name.equalsIgnoreCase("tem1")){
currentCity.setTem1(xmlPullParser.nextText());
}else if(name.equalsIgnoreCase("tem2")){
currentCity.setTem2(xmlPullParser.nextText());
}else if(name.equalsIgnoreCase("windState")){
currentCity.setWindState(xmlPullParser.nextText());
}
}
//Log.i(TAG, "------starttag-------"+name);
break;
case XmlPullParser.END_TAG:
//String names=xmlPullParser.getName();
//Log.i(TAG, "------endtag-------"+names);
//在标签结束时 进行添加到集合中
if(xmlPullParser.getName().equalsIgnoreCase("city")&¤tCity!=null){
//添加到集合中
entities.add(currentCity);
currentCity=null;
}
break;
}
//下一个
eventType=xmlPullParser.next();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return entities;
}
}
5、运行结果
6、Pull解析器的源码及文档下载网址:http://www.xmlpull.org/