Android从网络中获取xml文件并解析数据
1 public class XmlwebData 2 { 3 4 @SuppressLint("UseValueOf") 5 public static List<Person> getData(String path) throws Exception 6 { 7 URL url=new URL("http://192.168.5.10:8080/FileServer/person.xml"); 8 Person person=null; 9 List<Person> persons=null; 10 HttpURLConnection conn=(HttpURLConnection) url.openConnection(); 11 conn.setRequestMethod("GET"); 12 conn.setConnectTimeout(5000); 13 if(conn.getResponseCode()==200) 14 { 15 InputStream inputstream=conn.getInputStream(); 16 System.out.println("网络连接成功"); 17 18 XmlPullParser xml=Xml.newPullParser(); 19 xml.setInput(inputstream, "UTF-8"); 20 int event=xml.getEventType(); 21 22 while(event!=XmlPullParser.END_DOCUMENT) 23 { 24 switch (event) 25 { 26 //开始解析文档 27 case XmlPullParser.START_DOCUMENT: 28 persons=new ArrayList<Person>(); 29 break; 30 case XmlPullParser.START_TAG: 31 32 String value=xml.getName(); 33 if(value.equals("person")) 34 {//person对象的初始化必须在这里初始化不然可能出现为null的现象 35 person=new Person(); 36 //获取属性值 37 person.setId(new Integer(xml.getAttributeValue(0))); 38 } 39 else if(value.equals("name")) 40 { 41 person.setName(xml.nextText()); 42 System.out.println(person.getName()); 43 } 44 else if(value.equals("sex")) 45 { 46 person.setSex(xml.nextText()); 47 System.out.println(person.getSex()); 48 } 49 else if(value.equals("age")) 50 { 51 person.setAge(new Integer(xml.nextText())); 52 System.out.println(person.getAge()); 53 } 54 else if(value.equals("path")) 55 { 56 person.setPath(xml.nextText()); 57 System.out.println(person.getPath()); 58 } 59 break; 60 case XmlPullParser.END_TAG: 61 if(xml.getName().equals("person")) 62 { 63 persons.add(person); 64 person=null; 65 } 66 break; 67 } 68 //解析下一个对象 69 event=xml.next(); 70 } 71 } 72 return persons; 73 } 74 75 }
peson类
1 public class Person 2 { 3 private int id; 4 private String name; 5 private String sex; 6 private String path; 7 public String getPath() { 8 return path; 9 } 10 public void setPath(String path) { 11 this.path = path; 12 } 13 private int age; 14 public int getId() { 15 return id; 16 } 17 public void setId(int id) { 18 this.id = id; 19 } 20 public String getName() { 21 return name; 22 } 23 public void setName(String name) { 24 this.name = name; 25 } 26 public String getSex() { 27 return sex; 28 } 29 public void setSex(String sex) { 30 this.sex = sex; 31 } 32 public int getAge() { 33 return age; 34 } 35 public void setAge(int age) { 36 this.age = age; 37 } 38 public Person(){}; 39 public Person(int id, String name, String sex, int age,String path) { 40 super(); 41 this.id = id; 42 this.name = name; 43 this.sex = sex; 44 this.age = age; 45 this.path=path; 46 } 47 }
网络中的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>