日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

15_采用Pull解析器解析和生成XML内容

發(fā)布時間:2023/12/18 asp.net 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 15_采用Pull解析器解析和生成XML内容 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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();} }

?

//  單元測試

<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ù)結構

package cn.itcast.domain;public class Person {private Integer id;private String name;private Integer age;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}@Overridepublic String toString() {return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Person(Integer id, String name, Integer age) {this.id = id;this.name = name;this.age = age;}public Person() {}}

?

轉(zhuǎn)載于:https://www.cnblogs.com/carl2380/p/4159619.html

總結

以上是生活随笔為你收集整理的15_采用Pull解析器解析和生成XML内容的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。