数据解析1:XML解析(3)
XML解析的第二种方法是PULL解析。
PULL解析是以事件常量的的方法进行解析,何为事件常量?
当解析器从头到尾解析XML文件时,解析到文档开头定义为解析到一个常量XmlPullParser.START_DOCUMENT,当解析到开始标签定义为解析到一个常量XmlPullParser.START_TAG,当解析到结束标签定义为解析到一个常量XmlPullParser.END_TAG,当解析到文档结尾定义为解析到一个常量XmlPullParser.END_DOCUMENT。
下面是PULL解析的过程:
PULL解析的核心过程:
1 public class PullPaser1 { 2 3 public List<Person> pullParser() throws XmlPullParserException, IOException 4 { 5 List<Person> list = null; 6 Person person = null; 7 8 //1.创建解析器工厂对象 9 XmlPullParserFactory factory =XmlPullParserFactory.newInstance(); 10 //2.创建解析器对象 11 XmlPullParser parser = factory.newPullParser(); 12 //3.创建读取被解析的文件的字符读取流 13 //FileReader fileReader = new FileReader("user.xml"); 14 FileInputStream fis = new FileInputStream("user.xml"); 15 parser.setInput(fis, "utf-8");//可以设置编码 16 //得到解析时返回的第一个编号 17 int event = parser.getEventType(); 18 19 //4.具体的解析过程 20 while(event!=XmlPullParser.END_DOCUMENT) 21 { 22 switch(event) 23 { 24 case XmlPullParser.START_DOCUMENT: 25 list = new ArrayList<Person>();break; 26 case XmlPullParser.START_TAG: 27 String tagName = parser.getName();//得到开始标签名称 28 if("person".equals(tagName)) 29 { 30 person = new Person(); 31 } 32 else if("name".equals(tagName)) 33 person.setName(parser.nextText());//获取开始标签后边的数据 34 else if("age".equals(tagName)) 35 person.setAge(Integer.parseInt(parser.nextText())); 36 else if("sex".equals(tagName)) 37 person.setSex(parser.nextText()); 38 break; 39 case XmlPullParser.END_TAG: 40 if("person".equals(parser.getName())) 41 { 42 list.add(person); 43 } 44 break; 45 } 46 //获取下一个编号 47 event = parser.next(); 48 } 49 return list; 50 } 51 52 }
观察上面代码可知:PULL解析核心的过程和SAX解析很相似,对于SAX解析,是以事件通知的方法,即解析不同部分,调用不同的方法,而对于PULL解析,是以事件常量的方法,即解析不同部分,认为是不同的常量加以区分,执行不同的操作,而它们都需要先创建一个解析工厂,通过工厂来获取一个解析器对象。
PULL解析的步骤
1.获取PULL解析的工厂实例
XmlPullParserFactory factory =XmlPullParserFactory.newInstance();
2.通过工厂实例,获取PULL解析器对象
XmlPullParser parser = factory.newPullParser();
3.创建读取xml文件的流(若xml文件是本地),设置编码
FileInputStream fis = new FileInputStream("user.xml");
parser.setInput(fis, "utf-8");//可以设置编码
4.获取第一个事件常量,然后循环解析XML文件
int event = parser.getEventType();
while(event!=XmlPullParser.END_DOCUMENT)
{
switch(event)
{
case XmlPullParser.START_DOCUMENT:
...
break;
case XmlPullParser.START_TAG:
String tagName = parser.getName();//得到开始标签名称
....
break;
case XmlPullParser.END_TAG:
....
break;
}
//获取下一个编号
event = parser.next();
}
示例1:采用PULL解析user.xml文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <persons> 3 <person> 4 <name>张三</name> 5 <age>30</age> 6 <sex>男</sex> 7 </person> 8 9 <person> 10 <name>李四</name> 11 <age>32</age> 12 <sex>女</sex> 13 </person> 14 15 <person> 16 <name>王五</name> 17 <age>30</age> 18 <sex>男</sex> 19 </person> 20 </persons>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.qianfeng.pull1; 2 3 public class Person { 4 5 private String name; 6 private int age; 7 private String sex; 8 9 public Person(){} 10 11 public Person(String name, int age, String sex) { 12 super(); 13 this.name = name; 14 this.age = age; 15 this.sex = sex; 16 } 17 18 public String getName() { 19 return name; 20 } 21 22 public void setName(String name) { 23 this.name = name; 24 } 25 26 public int getAge() { 27 return age; 28 } 29 30 public void setAge(int age) { 31 this.age = age; 32 } 33 34 public String getSex() { 35 return sex; 36 } 37 38 public void setSex(String sex) { 39 this.sex = sex; 40 } 41 42 @Override 43 public String toString() { 44 return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]"; 45 } 46 47 48 49 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.qianfeng.pull1; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.FileReader; 6 import java.io.IOException; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import org.xmlpull.v1.XmlPullParser; 11 import org.xmlpull.v1.XmlPullParserException; 12 import org.xmlpull.v1.XmlPullParserFactory; 13 14 public class PullPaser1 { 15 16 public List<Person> pullParser() throws XmlPullParserException, IOException 17 { 18 List<Person> list = null; 19 Person person = null; 20 21 //创建解析器工厂对象 22 XmlPullParserFactory factory =XmlPullParserFactory.newInstance(); 23 //创建解析器对象 24 XmlPullParser parser = factory.newPullParser(); 25 //创建读取被解析的文件的字符读取流 26 //FileReader fileReader = new FileReader("user.xml"); 27 FileInputStream fis = new FileInputStream("user.xml"); 28 parser.setInput(fis, "utf-8");//可以设置编码 29 //得到解析时返回的第一个编号 30 int event = parser.getEventType(); 31 32 while(event!=XmlPullParser.END_DOCUMENT) 33 { 34 switch(event) 35 { 36 case XmlPullParser.START_DOCUMENT: 37 list = new ArrayList<Person>();break; 38 case XmlPullParser.START_TAG: 39 String tagName = parser.getName();//得到开始标签名称 40 if("person".equals(tagName)) 41 { 42 person = new Person(); 43 } 44 else if("name".equals(tagName)) 45 person.setName(parser.nextText());//获取开始标签后边的数据 46 else if("age".equals(tagName)) 47 person.setAge(Integer.parseInt(parser.nextText())); 48 else if("sex".equals(tagName)) 49 person.setSex(parser.nextText()); 50 break; 51 case XmlPullParser.END_TAG: 52 if("person".equals(parser.getName())) 53 { 54 list.add(person); 55 } 56 break; 57 } 58 //获取下一个编号 59 event = parser.next(); 60 } 61 return list; 62 } 63 64 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.qianfeng.pull1; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import org.xmlpull.v1.XmlPullParser; 7 import org.xmlpull.v1.XmlPullParserException; 8 9 public class Test { 10 11 /** 12 * @param args 13 * @throws IOException 14 * @throws XmlPullParserException 15 */ 16 public static void main(String[] args) throws XmlPullParserException, IOException { 17 // System.out.println(XmlPullParser.START_DOCUMENT); 18 // System.out.println(XmlPullParser.START_TAG); 19 // System.out.println(XmlPullParser.TEXT); 20 // System.out.println(XmlPullParser.END_TAG); 21 // System.out.println(XmlPullParser.END_DOCUMENT); 22 23 24 PullPaser1 pullParser1 = new PullPaser1(); 25 List<Person> list = pullParser1.pullParser(); 26 27 for(Person person:list) 28 { 29 System.out.println(person); 30 } 31 32 } 33 34 }
示例2:采用PULL解析worker.xml文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <workers> 3 <worker id="AQ01"> 4 <name>Mark</name> 5 <sex>男</sex> 6 <status>经理</status> 7 <address>北京</address> 8 <money>4000</money> 9 </worker> 10 <worker id="AD02"> 11 <name>Luch</name> 12 <sex>女</sex> 13 <status>员工</status> 14 <address>上海</address> 15 <money>1000</money> 16 </worker> 17 <worker id="AD03"> 18 <name>Lily</name> 19 <sex>女</sex> 20 <status>员工</status> 21 <address>北京</address> 22 <money>2000</money> 23 </worker> 24 <worker> 25 <name>Lily</name> 26 <sex>女</sex> 27 <status>员工</status> 28 <address>北京</address> 29 <money>2000</money> 30 </worker> 31 </workers>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.qianfeng.pull2; 2 3 public class Worker { 4 5 private String id; 6 private String name; 7 private char sex; 8 private String status; 9 private String address; 10 private double money; 11 12 public Worker(){} 13 14 public Worker(String id, String name, char sex, String status, 15 String address, double money) { 16 super(); 17 this.id = id; 18 this.name = name; 19 this.sex = sex; 20 this.status = status; 21 this.address = address; 22 this.money = money; 23 } 24 25 public String getId() { 26 return id; 27 } 28 29 public void setId(String id) { 30 this.id = id; 31 } 32 33 public String getName() { 34 return name; 35 } 36 37 public void setName(String name) { 38 this.name = name; 39 } 40 41 public char getSex() { 42 return sex; 43 } 44 45 public void setSex(char sex) { 46 this.sex = sex; 47 } 48 49 public String getStatus() { 50 return status; 51 } 52 53 public void setStatus(String status) { 54 this.status = status; 55 } 56 57 public String getAddress() { 58 return address; 59 } 60 61 public void setAddress(String address) { 62 this.address = address; 63 } 64 65 public double getMoney() { 66 return money; 67 } 68 69 public void setMoney(double money) { 70 this.money = money; 71 } 72 73 @Override 74 public String toString() { 75 return "Worker [id=" + id + ", name=" + name + ", sex=" + sex 76 + ", status=" + status + ", address=" + address + ", money=" 77 + money + "]"; 78 } 79 80 81 82 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.qianfeng.pull2; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 import org.xmlpull.v1.XmlPullParser; 10 import org.xmlpull.v1.XmlPullParserException; 11 import org.xmlpull.v1.XmlPullParserFactory; 12 13 public class PullParser2 { 14 15 public List<Worker> pullParser() throws XmlPullParserException, IOException 16 { 17 List<Worker> list = null; 18 Worker worker = null; 19 20 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 21 XmlPullParser parser = factory.newPullParser(); 22 23 FileInputStream fis = new FileInputStream("worker.xml"); 24 25 parser.setInput(fis, "utf-8"); 26 27 int event = parser.getEventType(); 28 29 while(event!=XmlPullParser.END_DOCUMENT){ 30 switch(event) 31 { 32 case XmlPullParser.START_DOCUMENT: 33 list = new ArrayList<Worker>(); 34 break; 35 case XmlPullParser.START_TAG: 36 String tagName = parser.getName(); 37 if("worker".equals(tagName)) 38 { 39 worker = new Worker(); 40 int count = parser.getAttributeCount();//得到属性数量 41 for(int i=0;i<count;i++) 42 { 43 //得到属性名 44 String attrName = parser.getAttributeName(i); 45 //得到属性值 46 String attrValue = parser.getAttributeValue(i); 47 48 if("id".equals(attrName)) 49 { 50 worker.setId(attrValue); 51 } 52 } 53 } 54 else if("name".equals(tagName)) 55 worker.setName(parser.nextText()); 56 else if("sex".equals(tagName)) 57 worker.setSex(parser.nextText().charAt(0)); 58 else if("status".equals(tagName)) 59 worker.setStatus(parser.nextText()); 60 else if("address".equals(tagName)) 61 worker.setAddress(parser.nextText()); 62 else if("money".equals(tagName)) 63 worker.setMoney(Double.parseDouble(parser.nextText())); 64 break; 65 case XmlPullParser.END_TAG: 66 if("worker".equals(parser.getName())) 67 { 68 list.add(worker); 69 } 70 break; 71 72 } 73 event = parser.next(); 74 } 75 76 return list; 77 } 78 79 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.qianfeng.pull2; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import org.xmlpull.v1.XmlPullParserException; 7 8 public class Test { 9 10 /** 11 * @param args 12 * @throws IOException 13 * @throws XmlPullParserException 14 */ 15 public static void main(String[] args) throws XmlPullParserException, IOException { 16 17 PullParser2 pullParser2 = new PullParser2(); 18 19 List<Worker> list = pullParser2.pullParser(); 20 21 for(Worker worker:list) 22 { 23 System.out.println(worker); 24 } 25 26 } 27 28 }
示例3:上次采用了SAX解析了citys_weather.xml,这次用PULL解析比较复杂的xml文件
citys_weather.xml文件见上篇数据解析1:XML解析(2)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.qianfeng.pull4; 2 3 import java.util.List; 4 5 public class Province { 6 7 private String id; 8 private String name; 9 private List<City> citys; 10 11 public Province(){} 12 13 public Province(String id, String name, List<City> citys) { 14 super(); 15 this.id = id; 16 this.name = name; 17 this.citys = citys; 18 } 19 20 public String getId() { 21 return id; 22 } 23 24 public void setId(String id) { 25 this.id = id; 26 } 27 28 public String getName() { 29 return name; 30 } 31 32 public void setName(String name) { 33 this.name = name; 34 } 35 36 public List<City> getCitys() { 37 return citys; 38 } 39 40 public void setCitys(List<City> citys) { 41 this.citys = citys; 42 } 43 44 @Override 45 public String toString() { 46 return name; 47 } 48 49 50 51 52 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.qianfeng.pull4; 2 3 import java.util.List; 4 5 public class City { 6 7 private String id; 8 private String name; 9 private List<District> districts; 10 11 public City(){} 12 13 public City(String id, String name, List<District> districts) { 14 super(); 15 this.id = id; 16 this.name = name; 17 this.districts = districts; 18 } 19 20 public String getId() { 21 return id; 22 } 23 24 public void setId(String id) { 25 this.id = id; 26 } 27 28 public String getName() { 29 return name; 30 } 31 32 public void setName(String name) { 33 this.name = name; 34 } 35 36 public List<District> getDistricts() { 37 return districts; 38 } 39 40 public void setDistricts(List<District> districts) { 41 this.districts = districts; 42 } 43 44 @Override 45 public String toString() { 46 return name; 47 } 48 49 50 51 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.qianfeng.pull4; 2 3 public class District { 4 5 private String id; 6 private String name; 7 8 public District(){} 9 10 public District(String id, String name) { 11 super(); 12 this.id = id; 13 this.name = name; 14 } 15 16 public String getId() { 17 return id; 18 } 19 20 public void setId(String id) { 21 this.id = id; 22 } 23 24 public String getName() { 25 return name; 26 } 27 28 public void setName(String name) { 29 this.name = name; 30 } 31 32 @Override 33 public String toString() { 34 return name; 35 } 36 37 38 39 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.qianfeng.pull4; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 import org.xmlpull.v1.XmlPullParser; 10 import org.xmlpull.v1.XmlPullParserException; 11 import org.xmlpull.v1.XmlPullParserFactory; 12 13 public class PullParser4 { 14 15 public List<Province> pullParser() throws XmlPullParserException, IOException 16 { 17 List<Province> provinces = null; 18 Province province = null; 19 List<City> citys = null; 20 City city = null; 21 List<District> districts = null; 22 District district = null; 23 24 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 25 XmlPullParser parser = factory.newPullParser(); 26 27 FileInputStream fis = new FileInputStream("citys_weather.xml"); 28 29 parser.setInput(fis, "utf-8"); 30 int event = parser.getEventType(); 31 while(event!=XmlPullParser.END_DOCUMENT) 32 { 33 switch(event) 34 { 35 case XmlPullParser.START_DOCUMENT: 36 provinces = new ArrayList<Province>();break; 37 case XmlPullParser.START_TAG: 38 String tagName = parser.getName(); 39 if("p".equals(tagName)) 40 { 41 province = new Province(); 42 citys = new ArrayList<City>(); 43 int count = parser.getAttributeCount(); 44 for(int i=0;i<count;i++) 45 { 46 String attrName = parser.getAttributeName(i); 47 String attrValue = parser.getAttributeValue(i); 48 if("p_id".equals(attrName)) 49 { 50 province.setId(attrValue); 51 } 52 } 53 } 54 if("pn".equals(tagName)) 55 province.setName(parser.nextText()); 56 if("c".equals(tagName)) 57 { 58 city = new City(); 59 districts = new ArrayList<District>(); 60 int count = parser.getAttributeCount(); 61 for(int i=0;i<count;i++) 62 { 63 String atrrName = parser.getAttributeName(i); 64 String attrValue = parser.getAttributeValue(i); 65 if("c_id".equals(atrrName)) 66 city.setId(attrValue); 67 } 68 } 69 if("cn".equals(tagName)) 70 city.setName(parser.nextText()); 71 if("d".equals(tagName)) 72 { 73 district = new District(); 74 int count = parser.getAttributeCount(); 75 for(int i=0;i<count;i++) 76 { 77 String atrrName = parser.getAttributeName(i); 78 String attrValue = parser.getAttributeValue(i); 79 if("d_id".equals(atrrName)) 80 district.setId(attrValue); 81 } 82 district.setName(parser.nextText()); 83 districts.add(district); 84 } 85 break; 86 case XmlPullParser.END_TAG: 87 if("c".equals(parser.getName())) 88 { 89 city.setDistricts(districts); 90 citys.add(city); 91 } 92 if("p".equals(parser.getName())) 93 { 94 province.setCitys(citys); 95 provinces.add(province); 96 } 97 break; 98 } 99 event = parser.next(); 100 } 101 return provinces; 102 } 103 104 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.qianfeng.pull4; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import org.xmlpull.v1.XmlPullParserException; 7 8 public class Test { 9 10 /** 11 * @param args 12 * @throws IOException 13 * @throws XmlPullParserException 14 */ 15 public static void main(String[] args) throws XmlPullParserException, IOException { 16 17 PullParser4 pullParser4 = new PullParser4(); 18 19 List<Province> list =pullParser4.pullParser(); 20 21 for(int i=0;i<list.size();i++) 22 { 23 Province province = list.get(i); 24 System.out.println("省:"+province); 25 List<City> citys = province.getCitys(); 26 for(int j=0;j<citys.size();j++) 27 { 28 City city = citys.get(j); 29 System.out.println("市===:"+city); 30 List<District> districts = city.getDistricts(); 31 for(int k =0;k<districts.size();k++) 32 { 33 District district =districts.get(k); 34 System.out.println("县=======:"+district); 35 } 36 } 37 } 38 39 40 41 } 42 43 }
未完,待续。