Android系统已经集成了Pull,所以不用导包。
@H_502_17@
@H_502_17@Xml解析:
@H_502_17@@H_502_17@public static @H_502_17@List<Person> @H_502_17@getPersons@H_502_17@(InputStream xml) @H_502_17@throws @H_502_17@Exception {
@H_502_17@List<Person> persons = @H_502_17@null;
@H_502_17@@H_502_17@Person person =
@H_502_17@// 获取xml的破解器,有下列两种方法
@H_502_17@//XmlPullParser pullParser = XmlPullParserFactory.newInstance().newPullParser();@H_502_17@XmlPullParser pullParser = Xml.@H_502_17@newPullParser@H_502_17@()@H_502_17@;@H_502_17@// 为破解器设置要解析的数据@H_502_17@pullParser.setInput(xml@H_502_17@,@H_502_17@"UTF-8"@H_502_17@)@H_502_17@
@H_502_17@// 首先将xmml数据读到字节数组中,,然后一个一个字符读取,判断<?xml version="1.0" encoding="UTF-8" ?>
// 是否符合XML中的某个语法,然后触发相应的事件。@H_502_17@@H_502_17@int @H_502_17@envnt = pullParser.getEventType()@H_502_17@while @H_502_17@(envnt != XmlPullParser.END_DOCUMENT@H_502_17@) { @H_502_17@//调用结束条件@H_502_17@switch @H_502_17@(envnt) {@H_502_17@case XmlPullParser.START_DOCUMENT:
@H_502_17@persons = @H_502_17@new @H_502_17@ArrayList<Person>()@H_502_17@break;@H_502_17@case XmlPullParser.START_TAG:if ("person".equals(pullParser.getName())) {//获取节点的属性值int id = new Integer(pullParser.getAttributeValue(0));person = Person()person.setId(id)}"name"//获取节点后面的textString name = pullParser.nextText()person.setName(name)"age"age = Integer(pullParser.nextText())person.setAge(age)
case END_TAG@H_502_17@persons.add(person)@H_502_17@person = @H_502_17@}@H_502_17@@H_502_17@//解析到<?xml version="1.0" encoding="UTF-8" ?>完成之后,解析器不会自动解析,如果想继续解析结点,调用如下方法@H_502_17@envnt = pullParser.next();@H_502_17@
@H_502_17@return @H_502_17@persons@H_502_17@}
@H_502_17@
@H_502_17@测试类:
@H_502_17@private static final @H_502_17@String @H_502_17@TAG @H_502_17@= @H_502_17@"TestDemo"@H_502_17@public void @H_502_17@testPersons@H_502_17@() @H_502_17@Exception{@H_502_17@InputStream is = this.getClass().getClassLoader().getResourceAsStream("person.xml");
@H_502_17@List<Person> persons = PersonServace.@H_502_17@getPersons@H_502_17@(is)@H_502_17@for@H_502_17@(Person p : persons){@H_502_17@Log.@H_502_17@i@H_502_17@(@H_502_17@TAG@H_502_17@p.toString())@H_502_17@}
@H_502_17@
@H_502_17@//保存数据到xml文件中@H_502_17@public static void @H_502_17@save@H_502_17@(List<Person> persons@H_502_17@OutputStream os) @H_502_17@//得到序列化器@H_502_17@XmlSerializer serializer = Xml.@H_502_17@newSerializer@H_502_17@serializer.setOutput(os@H_502_17@//设置输出@H_502_17@serializer.startDocument(@H_502_17@"UTF-8"@H_502_17@serializer.startTag(@H_502_17@null,255)">@H_502_17@"persons"//迭代集合,得到每个元素,生成person节点@H_502_17@serializer.startTag(null,)serializer.attribute("id",43); font-family:'Courier New'">p.getId().toString())
serializer.startTag(serializer.text(p.getName())serializer.endTag(serializer.text(p.getAge().toString());"persons"
@H_502_17@//结束文档serializer.endDocument();
@H_502_17@os.flush()@H_502_17@os.close()@H_502_17@}
@H_502_17@
@H_502_17@public void @H_502_17@testSave@H_502_17@() @H_502_17@throws @H_502_17@Exception{
@H_502_17@List<Person> persons = @H_502_17@new @H_502_17@ArrayList<>()@H_502_17@;@H_502_17@@H_502_17@persons.add(@H_502_17@Person(@H_502_17@19@H_502_17@,255); font-family:'Courier New'">@H_502_17@"yanxi"@H_502_17@10@H_502_17@))File xmlFile = File(getContext().getFilesDir()"itcast.xml"FileOutputStream fos = FileOutputStream(xmlFile)PersonServace.save(personsfos)@H_502_17@}