Pull 解析器简介
Pull 解析器的运行方式与 SAX 解析器相似。它提供了类似的事件,如: 开始元素和结束元素事件,使用xmlPullParser.next() 可以进入下一个元素并触发相应事件。跟 SAX 不同的 是, Pull 解析器产生的事件是一个数字,而非方法,因此可以使用一个 switch 对事件进行处理。当元素开始解析时,调用 parser.nextText() 方法可以获取下一个 Text 类型节点的值。
Pull解析器的源码及文档下载网址:http://www.xmlpull.org/
解析步骤
1.直接创建出XmlPullParser解析对象
2.设置解析文件输入流并且指定输入流在操作的编码方式
3.获取解析文件时返回的eventType时间类型
4.while循环遍历到文档结尾
5.使用xmlPullParser.next()进入下一个元素并触发
6.switch语句循环遍历结果
7.在标签结束时,进行添加到集合中
8.释放资源
Xml.newPullParser()--->setInput-->getEventType()--->while(type!=XmlPullParser.END_DOCUMENT)-->case
type --> parser.getName()去判断---->获得的信息添加进对象--->
type = parser.next();指针下移
实例代码:
资源文件china.xml
<?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="30" tem2="19" windState="西北风5-6级" /> <city> <cityname>河南</cityname> <pyName>henan</pyName> <quName>河南</quName> <state1>1</state1> <state2>1</state2> <stateDetailed>多云转晴</stateDetailed> <tem1>38</tem1> <tem2>-1</tem2> <windState>东南风2-3级</windState> </city> </china>
实体类City.java
package com.example.domain; public class City { private String cityname; private String pyName; private String quName; private String state1; private String state2; @Override public String toString() { return "City [cityname=" + cityname + ", pyName=" + pyName + ", quName=" + quName + ", state1=" + state1 + ", state2=" + state2 + ", stateDetailed=" + stateDetailed + ", tem1=" + tem1 + ", tem2=" + tem2 + ", windState=" + windState + "]"; } private String stateDetailed; private String tem1; private String tem2; private String windState; public String getCityname() { return cityname; } public City() { super(); } public void setCityname(String cityname) { this.cityname = cityname; } public String getPyName() { return pyName; } public void setPyName(String pyName) { this.pyName = pyName; } public String getQuName() { return quName; } public void setQuName(String quName) { this.quName = quName; } public String getState1() { return state1; } public void setState1(String state1) { this.state1 = state1; } public String getState2() { return state2; } public void setState2(String state2) { this.state2 = state2; } public String getStateDetailed() { return stateDetailed; } public void setStateDetailed(String stateDetailed) { this.stateDetailed = stateDetailed; } public String getTem1() { return tem1; } public void setTem1(String tem1) { this.tem1 = tem1; } public String getTem2() { return tem2; } public void setTem2(String tem2) { this.tem2 = tem2; } public String getWindState() { return windState; } public void setWindState(String windState) { this.windState = windState; } public City(String cityname, String pyName, String quName, String state1, String state2, String stateDetailed, String tem1, String tem2, String windState) { super(); this.cityname = cityname; this.pyName = pyName; this.quName = quName; this.state1 = state1; this.state2 = state2; this.stateDetailed = stateDetailed; this.tem1 = tem1; this.tem2 = tem2; this.windState = windState; } }
解析工具类PullXml.java
package com.example.util; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import android.util.Xml; import com.example.domain.City; public class PullXml { public List<City> pullXml() { List<City> entities = null; City currentCity = null; // 1.直接创建出XmlPullParser解析对象 XmlPullParser xmlPullParser = Xml.newPullParser(); try { // 2.设置解析文件输入流并且指定输入流在操作的编码方式 xmlPullParser.setInput(getClass().getClassLoader() .getResourceAsStream("china.xml"), "UTF-8"); // 3.获取解析文件时返回的eventType时间类型 int eventType = xmlPullParser.getEventType(); // while循环遍历到文档结尾 while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: entities = new ArrayList<City>(); break; case XmlPullParser.END_DOCUMENT: break; case XmlPullParser.START_TAG: String name = xmlPullParser.getName(); if (name.equals("city")) { // 声明当前的city对象 currentCity = new City(); int count = xmlPullParser.getAttributeCount(); if (count > 0) { /* * cityname="北京" pyName="beijing" quName="北京" * state1="1" state2="1" stateDetailed="多云" * tem1="30" tem2="19" windState="西北风5-6级" */ 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) { /* * <cityname>河南</cityname> <pyName>henan</pyName> * <quName>河南</quName> <state1>1</state1> * <state2>1</state2> * <stateDetailed>多云转晴</stateDetailed> <tem1>38</tem1> * <tem2>-1</tem2> <windState>东南风2-3级</windState> */ 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()); } } break; case XmlPullParser.END_TAG: String names = xmlPullParser.getName(); // 在标签结束时,进行添加到集合中 if (xmlPullParser.getName().equalsIgnoreCase("city") && currentCity != null) { // 添加到集合中 entities.add(currentCity); //释放资源 currentCity = null; } break; default: break; } // 使用xmlPullParser.next()进入下一个元素并触发 eventType = xmlPullParser.next(); } } catch (Exception e) { e.printStackTrace(); } return entities; } }
运行结果
用上篇讲解的测试方法测试程序结果 :