在Android應用中使用Pull解析XML檔案(傳智播客視頻筆記)

來源:互聯網
上載者:User

Service.java源碼:

package com.sinaapp.ssun.service;</p><p>import java.io.InputStream;<br />import java.io.OutputStream;<br />import java.util.*;</p><p>import org.xmlpull.v1.XmlPullParser;<br />import org.xmlpull.v1.XmlPullParserFactory;<br />import org.xmlpull.v1.XmlSerializer;</p><p>import android.util.Xml;</p><p>import com.sinaapp.ssun.domain.Person;</p><p>public class Service {<br />/**<br /> * 擷取XML檔案中的資料<br /> * @param xml<br /> * @return<br /> * @throws Exception<br /> */<br />public static List<Person> getPersons(InputStream xml) throws Exception {<br />List<Person> persons = null;<br />XmlPullParser parser = XmlPullParserFactory.newInstance()<br />.newPullParser();<br />// parser = Xml.newPullParser();<br />parser.setInput(xml, "UTF-8");<br />int event = parser.getEventType();<br />Person p = null;<br />while (event != XmlPullParser.END_DOCUMENT) {<br />switch (event) {<br />case XmlPullParser.START_DOCUMENT:<br />persons = new ArrayList<Person>();<br />break;<br />case XmlPullParser.START_TAG:<br />if("person".equals(parser.getName())){<br />p = new Person();<br />int id = Integer.parseInt(parser.getAttributeValue(0));<br />p.setId(id);<br />}<br />if("name".equals(parser.getName())){<br />String name = parser.nextText();<br />p.setName(name);<br />}<br />if("age".equals(parser.getName())){<br />int age = Integer.parseInt(parser.nextText());<br />p.setAge(age);<br />}<br />break;<br />case XmlPullParser.END_TAG:<br />if("person".equals(parser.getName())){<br />persons.add(p);<br />p = null;<br />}<br />break;<br />}<br />event = parser.next();<br />}<br />return persons;<br />}</p><p>/**<br /> * 儲存資料到XML檔案中<br /> * @param persons<br /> * @param out<br /> * @throws Exception<br /> */<br />public static void save(List<Person> persons , OutputStream out) throws Exception{<br />XmlSerializer serializer = Xml.newSerializer();<br />serializer.setOutput(out, "UTF-8");<br />serializer.startDocument("UTF-8", true);<br />serializer.startTag(null, "persons");<br />for(Person p: persons){<br />serializer.startTag(null, "person");<br />serializer.attribute(null, "person", p.getId()+"");</p><p>serializer.startTag(null, "name");<br />serializer.text(p.getName());<br />serializer.endTag(null, "name");</p><p>serializer.startTag(null, "age");<br />serializer.text(p.getAge()+"");<br />serializer.endTag(null, "age");</p><p>serializer.endTag(null, "person");<br />}<br />serializer.endTag(null, "persons");<br />serializer.endDocument();<br />out.flush();<br />out.close();<br />}<br />}</p><p>Person.java源碼:

package com.sinaapp.ssun.domain;</p><p>public class Person {<br />private String name;<br />private int age;<br />private int id;</p><p>public String getName() {<br />return name;<br />}<br />public void setName(String name) {<br />this.name = name;<br />}<br />public int getAge() {<br />return age;<br />}<br />public void setAge(int age) {<br />this.age = age;<br />}<br />public int getId() {<br />return id;<br />}<br />public void setId(int id) {<br />this.id = id;<br />}</p><p>public Person(String name, int age, int id) {<br />this.name = name;<br />this.age = age;<br />this.id = id;<br />}<br />public Person() {<br />super();<br />}<br />@Override<br />public String toString() {<br />return "Person [name=" + name + ", age=" + age + ", id=" + id + "]";<br />}<br />}<br />
text.xml檔案:

<!--test.xml--><br /><?xml version="1.0" encoding="UTF-8"?><!-- 開始文檔文法 --><br /><persons><br /> <person id="1"><br /> <name>ssun</name><br /> <age>19</age><br /> </person><br /> <person id="2"><br /> <name>cobe</name><br /> <age>24</age><br /> </person><br /></persons>
單元測試TestService.java源碼:

package com.sinaapp.ssun.test;</p><p>import java.io.File;<br />import java.io.FileNotFoundException;<br />import java.io.FileOutputStream;<br />import java.util.ArrayList;<br />import java.util.List;</p><p>import android.test.AndroidTestCase;<br />import android.util.Log;</p><p>import com.sinaapp.ssun.domain.Person;<br />import com.sinaapp.ssun.service.Service;</p><p>public class TestService extends AndroidTestCase {<br />private final String Tag = "Test";</p><p>public void testPersons() throws Exception{<br />List<Person> persons = Service.getPersons(this.getClass().getClassLoader().getResourceAsStream("test.xml"));<br />for(Person p : persons){<br />Log.i(Tag, p.toString());<br />}<br />}</p><p>public void testSave() throws Exception{<br />List<Person> persons = new ArrayList<Person>();<br />persons.add(new Person("www",19,23));<br />persons.add(new Person("hhh",19,3));<br />persons.add(new Person("qqq",19,24));<br />persons.add(new Person("ooo",19,25));<br />File file = new File(this.getContext().getFilesDir(),"test2.xml");<br />FileOutputStream out = new FileOutputStream(file);<br />Service.save(persons, out);<br />}</p><p>}<br />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.