1 public class XmlwebData
2 {
4 private static ArrayList persons=null; 6 public static ArrayList getData(final String path)
7 {
8 try
9 {
10 URL url=new URL(path);
11 Person person=null;
13 HttpURLConnection conn=(HttpURLConnection) url.openConnection();
14 conn.setRequestMethod("GET");
15 conn.setConnectTimeout(5000);
16 if(conn.getResponseCode()==200)
17 {
18 InputStream inputstream=conn.getInputStream();
21 XmlPullParser xml=Xml.newPullParser();
22 xml.setInput(inputstream, "UTF-8");
23 int event=xml.getEventType();
24
25 while(event!=XmlPullParser.END_DOCUMENT)
26 {
27 switch (event)
28 {
29 //开始解析文档
30 case XmlPullParser.START_DOCUMENT:
31 persons=new ArrayList();
32 break;
33 case XmlPullParser.START_TAG:
34
35 String value=xml.getName();
36 if(value.equals("person"))
37 {//person对象的初始化必须在这里初始化不然可能出现为null的现象
38 person=new Person();
39 //获取属性值
40 person.setId(new Integer(xml.getAttributeValue(0)));
41 }
42 else if(value.equals("name"))
43 {
44 person.setName(xml.nextText());
45 }
46 else if(value.equals("sex"))
47 {
48 person.setSex(xml.nextText());
49 }
50 else if(value.equals("age"))
51 {
52 person.setAge(new Integer(xml.nextText()));
53 }
54 else if(value.equals("path"))
55 {
56 person.setPath(xml.nextText());
57 }
58 break;
59 case XmlPullParser.END_TAG:
60 if(xml.getName().equals("person"))
61 {
62 persons.add(person);
63 System.out.println(person.getName());;
64 person=null;
65 }
66 break;
67 }
68 //解析下一个对象
69 event=xml.next();
70 }
71 return persons;
72 }
73 }
74 catch (Exception e)
75 {
76 e.printStackTrace();
77 }
78
79
80 return null;
81
82 }
83
84 }