pull xml解析

前端之家收集整理的这篇文章主要介绍了pull xml解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


pull解析

步骤:1xml文件放在src目录下方便取文件
2根据文件创建一个数据类
3创建一个数据服务类

输出数据:
1创建一个解析器XmlPullParser(这是一个接口得到对象有两种方式,一,通过解析工厂获得 二XML对象Xml.newPullParser()获得)
2解析时有几种状态:文件开始解析,标签开始解析,标签结束解析.......通过XmlPullParser对象的getEventType()获得状态
3由于pull解析不会一次加载完成,所以需要通过XmlPullParser对象next()移动
输入数据:
1XmlSerializer对象通过Xml.newSerializer()获得
2跟手写xml文件一样,先写文件头,然后就是标签等。再为标签添加属性

要解析的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<persons>
    <person id="23">
        <name>liming</name>
        <age>30</age>
    </person>
    
    <person id="24">
        <name>zhangsan</name>
        <age>25</age>
    </person>
</persons>

person类
package domain;

public class Person {
	
	
	private Integer id;
	private String name;
	private Integer  age;
	public Person(int id,String name,int age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public Person() {
		// TODO 自动生成的构造函数存根
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person [id=" + id + ",name=" + name + ",age=" + age + "]";
	}
}

解析服务类
package com.example.pullxml;

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 domain.Person;

public class PersonService {
	/*
	 * xml文件的一些介绍
	 * XmlPullParser是一个接口所以不能实例化。2种方法
	 * 标签 属性 标签内容值:
	 * 文件解析的几种状态:
	 * 进入时候pull解析不会一次全部加载:
	 * */
	//得到数据
	public static List<Person> getPerson(InputStream xml) throws Exception {
		
		Person person=null;
		XmlPullParser pullParser = Xml.newPullParser();
		pullParser.setInput(xml,"UTF-8");//设置格式
		ArrayList<Person> persons=null;
		int event = pullParser.getEventType();
		while (event!=XmlPullParser.END_DOCUMENT) {
			switch (event) {
			case XmlPullParser.START_DOCUMENT:
				persons = new ArrayList<Person>();
				break;
			case XmlPullParser.START_TAG:
				if("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;
			}
			event = pullParser.next();
		}
		return persons;
	}
	
	//输入数据
	public static void sava(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());
			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();
	}
}

测试类
package test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.test.AndroidTestCase;
import android.util.Log;

import com.example.pullxml.PersonService;

import domain.Person;

public class PersonServiceTest extends AndroidTestCase {
	private static final String TAG = "PersonServiceTest";
	
	//显示xml文件的数据
	public void test() throws Exception{
		InputStream xml=this.getClass().getClassLoader().getResourceAsStream("person.xml");
		List<Person> persons = PersonService.getPerson(xml);
		for(Person person:persons)
			Log.i(TAG,person.toString());
	}
	
	//添加内容放入xml文件当中
	public void sava()throws Exception{
		List<Person>  persons = new ArrayList<Person>();
		persons.add(new Person(32,"nikan",80));
		persons.add(new Person(31,30));
		File xmlfile = new File(getContext().getFilesDir(),"itcast.xml");
		FileOutputStream out = new FileOutputStream(xmlfile);
		PersonService.sava(persons,out);
	}
}

猜你在找的XML相关文章