Pull模式解析原理:html
與SAX解析器模擬類似,也提供了相似的事件驅動.使用parser.next()能夠進入下一個元素並觸發相應事件。事件將做爲數值代碼被髮送,所以可使用一個switch對感興趣的事件進行處理。當元素開始解析時,調用parser.nextText()方法能夠獲取下一個Text類型元素的值。java
Pull模式解析步驟:ide
一、 獲得一個XmlPullParser對象這裏獲得XMLPullParser對象的方式有兩種:工具
1)經過Xml這個工具類的.newPullParser()方式獲得 2)經過XmlPullParserFactory工廠建立
//獲得XmlPullParserFacotry工廠 [java] view plaincopyprint?測試
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser xmlparser = factory.newPullParser();
二、 再根據XmlPullParser對象設置輸入流和編碼格式 [java] view plaincopyprint?this
parser.setInput(inputStream, "UTF-8");
三、 而後循環遍歷全部的結點能夠經過文檔開始的代碼來判斷讀入到哪一個節點編碼
關於xmlparser.getEventType()其值有:spa
Start_DOCUMENT:文檔開始code
END_DOCUMENT:文檔結束xml
START_TAG:開始讀標籤
END_TAG:標籤結束
TEXT:所讀的是文本內容
一個簡單示例: [java] view plaincopyprint?
import java.io.IOException; import java.io.StringReader; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; public class SimpleXmlPullApp{ public static void main (String args[]) throws XmlPullParserException, IOException{ XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); XmlPullParser xpp = factory.newPullParser(); xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) ); int eventType = xpp.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if(eventType == XmlPullParser.START_DOCUMENT) { System.out.println("Start document"); } else if(eventType == XmlPullParser.START_TAG) { System.out.println("Start tag "+xpp.getName()); } else if(eventType == XmlPullParser.END_TAG) { System.out.println("End tag "+xpp.getName()); } else if(eventType == XmlPullParser.TEXT) { System.out.println("Text "+xpp.getText()); } eventType = xpp.next(); } System.out.println("End document"); } }
輸出結果:
Start document
Start tag foo
Text Hello World!
End tag foo
仍是以上一節的例子:讀取XML文件裏的內容把這些內容構形成一個Person對象,而後讀取這個XML文檔裏內容將其Person對象添加到一個List集合裏
person.xml: [html] view plaincopyprint?
<?xml version="1.0" encoding="UTF-8"?> <persons> <person id="23"> <name>李明</name> <age>30</age> </person> <person id="20"> <name>李向梅</name> <age>25</age> </person> </persons>
Person類: [java] view plaincopyprint?
public class Person { private Integer id; private String name; private short age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public short getAge() { return age; } public void setAge(short age) { this.age = age; } @Override public String toString() { return "Person [age=" + age + ", id=" + id + ", name=" + name + "]"; } }
業務bean [java] view plaincopyprint?
public static List<Person> readXml(InputStream inputStream) throws Exception { List<Person> persons=null; // XMLPullParser對象 XmlPullParser parser = Xml.newPullParser(); //設置輸入流和編碼 parser.setInput(inputStream, "UTF-8"); // 文件開始代碼 int eventCode = parser.getEventType(); Person person = null; while (eventCode != XmlPullParser.END_DOCUMENT) { switch (eventCode) { //文檔開始 case XmlPullParser.START_DOCUMENT: // 初始化 persons = new ArrayList<Person>(); break; case XmlPullParser.START_TAG: //開始標籤 if ("person".equals(parser.getName())) { person = new Person(); person.setId(Integer.parseInt(parser.getAttributeValue(0))); } else if(person!=null){ if ("name".equals(parser.getName())) { //nextNext()若是下一個元素的內容是文本的時候適合用 person.setName(parser.nextText()); } else if("age".equals(parser.getName())){ person.setAge(new Short(parser.nextText())); } } break; case XmlPullParser.END_TAG://結束標籤 if ("person".equals(parser.getName()) && person!=null) { persons.add(person); person=null;//再擲爲null } break; } // 下一個節點 eventCode = parser.next(); } return persons; }
測試: [java] view plaincopyprint?
public class SAXPersonServiceTest extends AndroidTestCase { private static final String TAG = "LogTest"; //PULL方式 public void testPULLReadXml() throws Exception { System.out.println("testPULLReadXml"); InputStream inputStream = SAXPersonServiceTest.class.getClassLoader() .getResourceAsStream("person.xml"); List<Person> list = PULLPersonService.readXml(inputStream); System.out.println(list.size()); for (Person person : list) { System.out.println(person); } } }