您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Pull方式解析XML的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Pull方式解析XML的示例分析”這篇文章吧。
Pull方式解析XML
1、XML文件
<?xml version="1.0" encoding="UTF-8"?> <persons> <person id="23"> <name>liming</name> <age>30</age> </person> <person id="20"> <name>lixiangmei</name> <age>25</age> </person> </persons>
2、實(shí)體類
package com.example.pullparsexml; public class Person { private Integer id; private String name; private Short age; public Person(){} public Person(Integer id, String name, Short age) { this.id = id; this.name = name; this.age = 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 + "]"; } }
3、代碼實(shí)現(xiàn)
package com.example.pullparsexml; import java.io.InputStream; import java.io.Writer; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlSerializer; import android.util.Xml; public class PullParseXML { public static void save(List<Person> persons, Writer writer) throws Throwable { XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(writer); serializer.startDocument("UTF-8", true); serializer.startTag(null, "persons"); for (Person person : persons) { serializer.startTag(null, "person"); serializer.attribute(null, "id", person.getId().toString()); serializer.startTag(null, "name"); serializer.text(person.getName()); serializer.endTag(null, "name"); serializer.startTag(null, "age"); serializer.text(person.getAge().toString()); serializer.endTag(null, "age"); serializer.endTag(null, "person"); } serializer.endTag(null, "persons"); serializer.endDocument(); writer.flush(); writer.close(); } public static List<Person> getPersons(InputStream inStream) throws Throwable { List<Person> persons = null; Person person = null; XmlPullParser parser = Xml.newPullParser(); parser.setInput(inStream, "UTF-8"); int eventType = parser.getEventType();// 產(chǎn)生第一個(gè)事件 while (eventType != XmlPullParser.END_DOCUMENT) {// 只要不是文檔結(jié)束事件 switch (eventType) { case XmlPullParser.START_DOCUMENT: persons = new ArrayList<Person>(); break; case XmlPullParser.START_TAG: String name = parser.getName();// 獲取解析器當(dāng)前指向的元素的名稱 if ("person".equals(name)) { person = new Person(); person.setId(new Integer(parser.getAttributeValue(0))); } if (person != null) { if ("name".equals(name)) { person.setName(parser.nextText());// 獲取解析器當(dāng)前指向元素的下一個(gè)文本節(jié)點(diǎn)的值 } if ("age".equals(name)) { person.setAge(new Short(parser.nextText())); } } break; case XmlPullParser.END_TAG: if ("person".equals(parser.getName())) { persons.add(person); person = null; } break; } eventType = parser.next(); } return persons; } }
以上是“Pull方式解析XML的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。