博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在Android应用中使用Pull解析XML文件(传智播客视频笔记)
阅读量:5949 次
发布时间:2019-06-19

本文共 3946 字,大约阅读时间需要 13 分钟。

Service.java源码:

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

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

<!--test.xml--> <?xml version="1.0" encoding="UTF-8"?><!-- 开始文档语法 --> <persons> <person id="1"> <name>ssun</name> <age>19</age> </person> <person id="2"> <name>cobe</name> <age>24</age> </person> </persons>
单元测试TestService.java源码:

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

转载地址:http://nksxx.baihongyu.com/

你可能感兴趣的文章
VC++深入详解学习笔记1
查看>>
对OpenBSD中的OSPF和BGP路由协议的探索
查看>>
安装配置discuz
查看>>
CentOS7 64位小型操作系统的安装
查看>>
线程互互斥锁
查看>>
KVM虚拟机&openVSwitch杂记(1)
查看>>
使用Jmeter进行http接口测试
查看>>
win7下ActiveX注册错误0x80040200解决参考
查看>>
python自动化创建mysql多实例
查看>>
《.NET应用架构设计:原则、模式与实践》新书博客--试读-1.1-正确认识软件架构...
查看>>
网址收藏
查看>>
SSDB无法连接问题分析
查看>>
Vmware安装报msi错误解决方案(官方翻译中文版)
查看>>
2013 Linux领域年终盘点
查看>>
私有云搭建 OpenStack(centos7.3, centos-release-openstack-liberty) (中篇)
查看>>
Bootstrap3 表单-被支持的控件:输入框
查看>>
Bootstrap3 表单-基本表单
查看>>
【翻译】如何在Ext JS 6中使用Fashion美化应用程序
查看>>
(转载)浅谈javascript中的原型和继承
查看>>
删除存储
查看>>