Java数据解析---PULL
安卓和JAVA解析xml文件的三种方式:
1、PULL解析
2、SAX解析
3、DOM解析
三者各有所长,依情况选择解析方式
1、PULL和SAX均采用流式解析,意味着只能从头读到底,无法像DOM解析一样随机访问xml文件中的任意一个节点
2、PULL和SAX占用更少的内存解析,更适用于安卓上的解析xml文件
3、DOM解析时将文件全部解析完,最后让用户任意取特定的信息
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
下面学习PULL解析方式。(PULL方法的简洁性是它最大的优势)
1、解析本地xml文件
1 public class PullParser { 2 3 public List<Worker> pullParsers() throws XmlPullParserException, IOException 4 { 5 List<Worker> list = null; 6 Worker worker = null; 7 //第一步:创建XML解析对象,需要通过工厂模式创建类的实例 8 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 9 XmlPullParser parser = factory.newPullParser(); 10 // 第二步:设置待解析的xml数据源 11 FileInputStream fis = new FileInputStream("worker.xml"); 12 parser.setInput(fis,"utf-8"); 13 // 第三步:获取当前xml标签的事件类型 14 int event = parser.getEventType(); 15 // 第四步:通过分支语句处理不同的事件标签 16 while(event!=XmlPullParser.END_DOCUMENT) 17 { 18 19 switch(event) 20 { 21 // 第五步:获取标签名称,处理标签信息 22 case XmlPullParser.START_DOCUMENT://解析文件开始,创建列表集合对象 23 list = new ArrayList<Worker>(); 24 break; 25 case XmlPullParser.START_TAG: 26 String tagName = parser.getName(); 27 if("worker".equals(tagName)) 28 { 29 worker = new Worker(); 30 int count = parser.getAttributeCount();//得到属性的个数 31 for(int i=0;i<count;i++) 32 { 33 String attrName = parser.getAttributeName(i); 34 String attrValue = parser.getAttributeValue(i); 35 if("id".equals(attrName)) 36 worker.setId(attrValue); 37 } 38 } 39 else if("name".equals(tagName)) 40 worker.setName(parser.nextText()); 41 worker.setMoney(Double.parseDouble(parser.nextText())); 42 break; 43 44 case XmlPullParser.END_TAG://解析文件结束, 45 if("worker".equals(parser.getName())) 46 { 47 // 第六步:将处理的数据保存到JavaBean或Map对象中 48 list.add(worker); 49 worker = null; 50 } 51 break; 52 } 53 // 第七步:获取下一个标签的事件类型,判断事件类型是否为文档结束事件,如果是则退出,反之重复第四、五、六步 54 event = parser.next(); 55 } 56 return list; 57 } 58 59 /** 60 * @param args 61 * @throws IOException 62 * @throws XmlPullParserException 63 */ 64 public static void main(String[] args) throws XmlPullParserException, IOException { 65 PullParser pullParser = new PullParser(); 66 List<Worker> list = pullParser.pullParsers(); 67 68 for(Worker w:list) 69 { 70 System.out.println(w); 71 } 72 } 73 74 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <workers> 3 <worker id="AQ01"> 4 <name>Mark</name> 5 </worker> 6 <worker id="AD02"> 7 <name>Luch</name> 8 </worker> 9 <worker id="AD03"> 10 <name>Lily</name> 11 </worker> 12 <worker id="AD04"> 13 <name>Lily</name> 14 </worker> 15 </workers>
1 package com.qianfeng.pullparser2; 2 3 public class Worker { 4 5 private String id; 6 private String name; 7 8 public Worker() { 9 super(); 10 // TODO Auto-generated constructor stub 11 } 12 public Worker(String id, String name) { 13 super(); 14 this.id = id; 15 this.name = name; 16 17 } 18 public String getId() { 19 return id; 20 } 21 public void setId(String id) { 22 this.id = id; 23 } 24 public String getName() { 25 return name; 26 } 27 public void setName(String name) { 28 this.name = name; 29 } 30 31 @Override 32 public String toString() { 33 return "Worker [id=" + id + ", name=" + name + "]"; 34 } 35 }
解析效果:
1 Worker [id=AQ01, name=Mark] 2 Worker [id=AD02, name=Luch] 3 Worker [id=AD03, name=Lily] 4 Worker [id=AD04, name=Lily]
2、解析服务器的xml文件
1 package pull; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import org.xmlpull.v1.XmlPullParser; 9 import org.xmlpull.v1.XmlPullParserException; 10 import org.xmlpull.v1.XmlPullParserFactory; 11 12 public class Pull { 13 14 /** 15 * @param args 16 * @throws XmlPullParserException 17 * @throws IOException 18 */ 19 public static void main(String[] args) throws XmlPullParserException, IOException { 20 // TODO Auto-generated method stub 21 List<Person> list = null; 22 Person person=null; 23 24 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 25 XmlPullParser parser = factory.newPullParser(); 26 String path ="http://localhost:8080/aaa/person.xml"; 27 InputStream in = HttpUtil.getInputStream(path); 28 parser.setInput(in, "utf-8"); 29 int event = parser.getEventType(); 30 while(event!=XmlPullParser.END_DOCUMENT) 31 { 32 switch(event) 33 { 34 case XmlPullParser.START_DOCUMENT: 35 System.out.println("文件解析开始"); 36 list = new ArrayList<Person>(); 37 break; 38 case XmlPullParser.START_TAG: 39 String str = parser.getName(); 40 if("person".equals(str)) 41 { 42 person = new Person(); 43 int count = parser.getAttributeCount(); 44 for(int i=0;i<count;i++) 45 { 46 String key = parser.getAttributeName(i); 47 String value = parser.getAttributeValue(i); 48 if("id".equals(key)) 49 { 50 person.setName(value); 51 } 52 } 53 } 54 else if("name".equals(str)) 55 { 56 person.setName(parser.nextText()); 57 } 58 break; 59 case XmlPullParser.END_TAG: 60 if("person".equals(parser.getName())) 61 { 62 list.add(person); 63 person=null; 64 } 65 break; 66 } 67 event = parser.next(); 68 69 } 70 71 for(int i=0;i<list.size();i++) 72 { 73 System.out.println(list); 74 } 75 76 77 } 78 79 }
对应的HttpUtil自定义类getInpustStream方法
1 package pull; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.net.HttpURLConnection; 6 import java.net.MalformedURLException; 7 import java.net.URL; 8 9 public class HttpUtil { 10 public static InputStream getInputStream(String path) throws IOException{ 11 URL url = new URL(path); 12 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 13 conn.setRequestMethod("GET"); 14 conn.setConnectTimeout(5000); 15 conn.setDoInput(true); 16 if(conn.getResponseCode() ==200) 17 { 18 InputStream in = conn.getInputStream(); 19 return in; 20 } 21 22 23 return null; 24 25 } 26 27 }
解析效果:
1 文件解析开始 2 [Pserson [id=0, name=张老师, age=0], Pserson [id=0, name=李老师, age=0]] 3 [Pserson [id=0, name=张老师, age=0], Pserson [id=0, name=李老师, age=0]]
作者:听着music睡
出处:http://www.cnblogs.com/xqxacm/
Android交流群:38197636
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。