网络 xml 解析方式
1 package com.example.dashu_saxxml; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.HashMap; 6 import java.util.List; 7 8 import javax.xml.parsers.SAXParser; 9 import javax.xml.parsers.SAXParserFactory; 10 11 import org.xml.sax.InputSource; 12 import org.xml.sax.SAXParseException; 13 14 public class SaxService { 15 public SaxService() { 16 } 17 18 /** 19 * 网络xml解析方式 20 * */ 21 public static List<HashMap<String, String>> readXML( 22 InputStream inputStream, String nodeName) { 23 List<HashMap<String, String>>list=null; 24 try { 25 //创建一个解析xml工厂对象 26 SAXParserFactory saxParserFactory=SAXParserFactory.newInstance(); 27 SAXParser parser=saxParserFactory.newSAXParser();//解析xml 28 MyHandler myHandler=new MyHandler(nodeName); 29 parser.parse(inputStream, myHandler); 30 list=myHandler.getList(); 31 inputStream.close();//关闭io 32 } catch (Exception e) { 33 // TODO: handle exception 34 } 35 return list; 36 } 37 }
MyHandler 继承 DefaultHandler 解析工具类
1 package com.example.dashu_saxxml; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 7 import org.xml.sax.Attributes; 8 import org.xml.sax.SAXException; 9 import org.xml.sax.helpers.DefaultHandler; 10 11 import android.R.integer; 12 13 public class MyHandler extends DefaultHandler { 14 private HashMap<String, String> map = null;// 存储单个解析的完整对象 15 List<HashMap<String, String>> list = null;// 存储所以的解析对象 16 private String currentTag = null;// 正在解析元素的标签 17 private String currentValue = null;// 正在解析元素的值 18 private String nodeName = null;// 当前解析节点名称 19 20 public MyHandler(String nodeName) { 21 super(); 22 this.nodeName = nodeName; 23 } 24 25 public List<HashMap<String, String>> getList() { 26 return list; 27 } 28 29 /** 30 * xml文件开始解析时候调用的方法 31 * */ 32 @Override 33 public void startDocument() throws SAXException { 34 // TODO Auto-generated method stub 35 list = new ArrayList<HashMap<String, String>>(); 36 super.startDocument(); 37 } 38 39 /** 40 * 解析到节点开头调用方法<name> 41 * */ 42 @Override 43 public void startElement(String uri, String localName, String qName, 44 Attributes attributes) throws SAXException { 45 // TODO Auto-generated method stub 46 if (qName.equals(nodeName)) { 47 map = new HashMap<String, String>(); 48 } 49 if (attributes != null && map != null) { 50 for (int i = 0; i < attributes.getLength(); i++) { 51 map.put(attributes.getQName(i), attributes.getValue(i)); 52 } 53 } 54 currentTag = qName; 55 } 56 57 /** 58 * 解析到节点开头结尾中间夹的文字所调用的方法 59 * */ 60 @Override 61 public void characters(char[] ch, int start, int length) 62 throws SAXException { 63 // TODO Auto-generated method stub 64 if (currentTag != null && map != null) { 65 currentValue = new String(ch, start, length); 66 if (currentValue != null && !currentValue.trim().equals("") 67 && !currentValue.trim().equals("\n")) { 68 map.put(currentTag, currentValue); 69 } 70 } 71 currentTag = null;// 把当前节点对应的值和标签设置为空 72 currentValue = null; 73 } 74 75 /** 76 * 解析到节点结尾调用方法</name> 77 * */ 78 @Override 79 public void endElement(String uri, String localName, String qName) 80 throws SAXException { 81 // 遇到结束标记时候 82 if (qName.equals(nodeName)) { 83 list.add(map); 84 map = null; 85 } 86 } 87 }
dashu.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <user-list> 3 <user id="1"> 4 <name>大树</name> 5 <age>22</age> 6 </user> 7 8 <user id="2"> 9 <name>梅子</name> 10 <age>28</age> 11 </user> 12 13 <user id="3"> 14 <name>爸爸</name> 15 <age>48</age> 16 </user> 17 18 <user id="4"> 19 <name>妈妈</name> 20 <age>48</age> 21 </user> 22 </user-list>
【参考文献】
http://blog.csdn.net/u010794950/article/details/24553733
时间: 2024-10-13 11:35:04