PULL方法解析XML文件

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

需要用到kxml2-2.2.2.jar包

要解析的XML文件内容

<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="1">
<name>张三</name>
<age>34</age>
</person>
<person id ="2">
<name>李四</name>
<age>24</age>
</person>
</persons>

Person.java文件内容

public class Person {

	private int id;
	private String name;
	private int age;
	public Person() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public String toString() {
		return "Person [id=" + id + ",name=" + name + ",age=" + age + "]";
	}

	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 int getId() {
		return id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getAge() {
		return age;
	}

}

PullXMLTools.java解释工具类

public class PullXMLTools {

	public PullXMLTools() {
		// TODO Auto-generated constructor stub
	}
	/**
	 * @param inputStream 从服务器获取xml文件,以流的形式返回
	 * @param encode编号格式需与xml文件一致,UTF-8
	 * @return
	 * @throws XmlPullParserException 
	 * @throws IOException 
	 */
    public static List<Person> parseXML(InputStream inputStream,String encode) throws XmlPullParserException,IOException{
    	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();
    	System.out.println("eventType="+eventType);
    	System.out.println("XmlPullParser.END_DOCUMEN="+ XmlPullParser.END_DOCUMENT);
    	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();
					//取出属性值
					int id = Integer.parseInt(parser.getAttributeValue(0));
					person.setId(id);					
				}else if("name".equals(parser.getName())){
					String name = parser.nextText();//获取该节点的内容
					person.setName(name);
				}else if("age".equals(parser.getName())){
					int age = Integer.parseInt(parser.nextText());
					person.setAge(age);
				}
				break;
			case XmlPullParser.END_TAG:
				if("person".equals(parser.getName())){
					list.add(person);
					person = null;
				}
			
				break;
			}
    		eventType = parser.next();
    	}
    	return list;
    }
 }

HttpUtils.java

/**
 * 从服务器获得xml文件返回流
 * @author Administrator
 *
 */
public class HttpUtils {

	public HttpUtils() {
		// TODO Auto-generated constructor stub
	}
	public static InputStream getXML(String path){
		System.out.println("进到httpUtils");
		InputStream inputStream = null;
		try {
			URL url = new URL(path);
			System.out.println("url="+url.toString());
			if(url != null){
				HttpURLConnection connection = (HttpURLConnection) url.openConnection();
				connection.setConnectTimeout(3000);//延时时间3秒
				connection.setDoInput(true);// 设置是否从httpUrlConnection读入,默认情况下是true; 
				connection.setRequestMethod("GET");
				int code = connection.getResponseCode();//验证连接服务器是否成功,没有密码时,返回200为成功,有密码时返回401为成功
				System.out.println("code="+code);
				if(code == 200){
					inputStream =  connection.getInputStream();
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return inputStream;
	}
}

Text.java测试类

public class Text {

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

	/**
	 * @param args
	 * @throws IOException 
	 * @throws XmlPullParserException 
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	  String path="http://chinese-1c71864:8080/android_pull_xml/person.xml";
	  //获取xml内容,返回InputStream 流
      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();
	} 
      
	}

}

猜你在找的XML相关文章