标签:
具体结构图参考sax
public class Person { private int id; private String name; private int age; public int getId() { return id; } 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 } public Person(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
解析类
import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import com.pull.domian.Person; /** * 主要是使用Pull解析xml * * @author zy * */ public class PullXMLTools { public PullXMLTools() { // TODO Auto-generated constructor stub } /** * * @param inputstream * 从服务器获取xml文件,以流的形式返回 * @param encode * 编码格式 * @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(); //设置输入流 xml文件 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; } }
工具类,获得输入流
import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class HttpUtils { public HttpUtils() { // 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; } }
测试类
import java.io.InputStream; import java.util.List; import com.pull.domian.Person; import com.pull.http.HttpUtils; import com.pull.parser.PullXMLTools; public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String path = "http://169.254.32.172:8080/myhttp/person.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: handle exception e.printStackTrace(); } } }
结果:
Person [id=1, name=张三, age=23]
Person [id=2, name=李四, age=25]
标签:
原文地址:http://www.cnblogs.com/zyxiaohuihui/p/4537961.html