Android Xml解析之Pull

//Test.java

public class Test {

	public Test() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String path = "http://localhost:8080/myhttp/persion.xml";
		InputStream inputStream = HttpUtils.getXML(path);
		List<Person> list = null;
		try {
			list = PullXMLTools.parseXML(inputStream, "utf-8");
			for (Person person : list) {
				System.out.println(person.toString());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
//HttpUtils .java


public class HttpUtils {

	/**
	 * 从服务器获得xml文件返回流
	 * @param path
	 * @return
	 */
	public static InputStream getXML(String path) {
		InputStream inputStream = null;
		try {
			URL url = new URL(path);
			if (url != null) {
				HttpURLConnection connection = (HttpURLConnection) url
						.openConnection();
				connection.setConnectTimeout(3000);
				connection.setDoInput(true);
				connection.setRequestMethod("GET");
				int code = connection.getResponseCode();
				if (code == 200) {
					inputStream = connection.getInputStream();
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return inputStream;
	}
}


/**
 * 主要是使用PULL解析xml
 * 
 * @author jack
 * 
 */
public class PullXMLTools {

	public PullXMLTools() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param inputStream
	 *            从服务器获取xml文件,以流的形式返回
	 * @param encode
	 *            编码格式
	 * @return
	 * @throws Exception
	 */
	public static List<Person> parseXML(InputStream inputStream, String encode)
			throws Exception {
		List<Person> list = null;
		Person person = null;// 装载解析每一个person节点的内容
		// 创建一个xml解析的工厂
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		// 获得xml解析类的引用
		XmlPullParser parser = factory.newPullParser();
		parser.setInput(inputStream, encode);
		// 获得事件的类型
		int eventType = parser.getEventType();
		while (eventType != XmlPullParser.END_DOCUMENT) {
			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
				list = new ArrayList<Person>();
				break;
			case XmlPullParser.START_TAG:
				if ("person".equals(parser.getName())) {
					person = new Person();
				} 
				if ("name".equals(parser.getName())) {
					String name = parser.nextText();// 获取该节点的内容
					person.setName(name);
				}
				if ("age".equals(parser.getName())) {
					int age = Integer.parseInt(parser.nextText());
					person.setAge(age);
				}
				if ("id".equals(parser.getName())) {
					int id = Integer.parseInt(parser.nextText());
					person.setId(id);
				}
				break;
			case XmlPullParser.END_TAG:
				if ("person".equals(parser.getName())) {
					list.add(person);
					person = null;
				}
				break;
			}
			eventType = parser.next();
		}
		return list;
	}

}

//Person.java


package com.pull.domain;

public class Person {

	private int id;
	private String name;
	private int age;

	public int getId() {
		return id;
	}

	public Person(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public void setId(int id) {
		this.id = 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 Person() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值