标签:
PullXmlTools.java
package com.toolsdemo; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserFactory; /* * 主要是使用pull解析xml * @author jack */ public class PullXMLTools { public PullXMLTools() { // TODO Auto-generated constructor stub } 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(); //取出属性值 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; } }
Person.java
package com.toolsdemo; public class Person { private int id; private String name; private int age; public Person(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public int getId() { return id; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", 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 } }
HttpUtil.java
package com.toolsdemo; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class HttpUtil { public HttpUtil() { // TODO Auto-generated constructor stub } 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; } }
Test.java
package com.toolsdemo; import java.io.InputStream; import java.util.List; public class Test { public Test() { // TODO Auto-generated constructor stub } /** * @param args */ public static void main(String[] args) { String path="http://weikew.gotoip55.com/person.xml"; InputStream inputstream=HttpUtil.getXML(path); try { List<Person> list=PullXMLTools.parseXML(inputstream, "utf-8"); for(Person person:list) { System.out.println(person.toString()); } } catch (Exception e) { // TODO: handle exception } } }
xml
<?xml version="1.0" encoding="utf-8"?> <persons> <person id="20"> <name>张老进</name> <age>22</age> </person> <person id="33"> <name>李法在</name> <age>44</age> </person> </persons>
标签:
原文地址:http://www.cnblogs.com/zywf/p/4844884.html