public class XmlwebData
{ @SuppressLint("UseValueOf")
public static List<Person> getData(String path) throws Exception
{
URL url=new URL("http://192.168.5.10:8080/FileServer/person.xml");
Person person=null;
List<Person> persons=null;
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
if(conn.getResponseCode()==200)
{
InputStream inputstream=conn.getInputStream();
System.out.println("网络连接成功"); XmlPullParser xml=Xml.newPullParser();
xml.setInput(inputstream, "UTF-8");
int event=xml.getEventType(); while(event!=XmlPullParser.END_DOCUMENT)
{
switch (event)
{
//开始解析文档
case XmlPullParser.START_DOCUMENT:
persons=new ArrayList<Person>();
break;
case XmlPullParser.START_TAG: String value=xml.getName();
if(value.equals("person"))
{//person对象的初始化必须在这里初始化不然可能出现为null的现象
person=new Person();
//获取属性值
person.setId(new Integer(xml.getAttributeValue(0)));
}
else if(value.equals("name"))
{
person.setName(xml.nextText());
System.out.println(person.getName());
}
else if(value.equals("sex"))
{
person.setSex(xml.nextText());
System.out.println(person.getSex());
}
else if(value.equals("age"))
{
person.setAge(new Integer(xml.nextText()));
System.out.println(person.getAge());
}
else if(value.equals("path"))
{
person.setPath(xml.nextText());
System.out.println(person.getPath());
}
break;
case XmlPullParser.END_TAG:
if(xml.getName().equals("person"))
{
persons.add(person);
person=null;
}
break;
}
//解析下一个对象
event=xml.next();
}
}
return persons;
} }

peson类

 public class Person
{
private int id;
private String name;
private String sex;
private String path;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(){};
public Person(int id, String name, String sex, int age,String path) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.path=path;
}
}

网络中的person.xml文件的数据为

<?xml version="1.0" encoding="UTF-8"?>
<Persons>
<person id="1">
<name>张三</name>
<sex>男</sex>
<age>25</age>
<path>http://192.168.5.10:8080/FileServer/chengjisihan.jpg</path>
</person>
<person id="2">
<name>李斯</name>
<sex>男</sex>
<age>78</age>
<path>http://192.168.5.10:8080/FileServer/laozi.jpg</path>
</person>
<person id="3">
<name>王五</name>
<sex>男</sex>
<age>22</age>
<path>http://192.168.5.10:8080/FileServer/lilongji.jpg</path>
</person>
</Persons>
05-06 20:21