15_采用Pull解析器解析和生成XML内容
生活随笔
收集整理的這篇文章主要介紹了
15_采用Pull解析器解析和生成XML内容
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
java還提供SAX和DOM用于解析XML
Android還集成了Pull解析器——推薦
?
package cn.itcast.service;import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlSerializer;import android.util.Xml;import cn.itcast.domain.Person;public class PersonService {/*** 讀取數(shù)據(jù)* @param xml* @return* @throws Exception*/public static List<Person> getPersons(InputStream xml) throws Exception{List<Person> persons = null;Person person = null;XmlPullParser pullparser = Xml.newPullParser();pullparser.setInput(xml, "UTF-8");int event = pullparser.getEventType();while (event!=pullparser.END_DOCUMENT){switch (event) {case XmlPullParser.START_DOCUMENT:// 數(shù)據(jù)初始化persons = new ArrayList<Person>();break;case XmlPullParser.START_TAG:// 數(shù)據(jù)readif ("person".equals(pullparser.getName())){int id = new Integer(pullparser.getAttributeValue(0));person = new Person();person.setId(id);}if ("name".equals(pullparser.getName())) {String name = pullparser.nextText();person.setName(name);}if ("age".equals(pullparser.getName())) {int age = new Integer(pullparser.nextText());person.setAge(age);} break;case XmlPullParser.END_TAG:if ("person".equals(pullparser.getName())){persons.add(person);}break;default:break;} event = pullparser.next(); }return persons;}/*** 保存數(shù)據(jù)* @param persons* @param out* @throws Exception*/public static void savePersons(List<Person> persons, OutputStream out) throws Exception{XmlSerializer serializer = Xml.newSerializer();serializer.setOutput(out, "UTF-8");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().toString());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();out.flush();out.close();} }?
// 單元測(cè)試
<instrumentationandroid:name="android.test.InstrumentationTestRunner"android:targetPackage="cn.itcast.xml" /><application<uses-library android:name="android.test.runner" />?
public class PersonServiceTest extends AndroidTestCase {public void testPersons() throws Exception{ InputStream xml = this.getClass().getClassLoader().getResourceAsStream("person.xml");List<Person> persons = PersonService.getPersons(xml);for (Person person:persons){Log.i("test111", person.toString());} }public void testSave() throws Exception{List<Person> persons = new ArrayList<Person>();persons.add(new Person(10, "10", 10));persons.add(new Person(20, "20", 20)); File xml = new File(getContext().getFilesDir(), "itcast.xml");FileOutputStream outputStream = new FileOutputStream(xml); PersonService.savePersons(persons, outputStream); outputStream.close(); } }
// 數(shù)據(jù)結(jié)構(gòu)
?
轉(zhuǎn)載于:https://www.cnblogs.com/carl2380/p/4159619.html
總結(jié)
以上是生活随笔為你收集整理的15_采用Pull解析器解析和生成XML内容的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: postfix导致maillog填满磁盘
- 下一篇: 停一下