解析XML文档之一:使用SAX解析
使用sax解析xml方法总结
解析的的xml文档格式如下
<?xml version="1.0" encoding = "UTF-8"?> <workers> <worker id = "A001"> <name>Mark</name> <sex>male</sex> <status>project manager送的积分了送积分了送多了几分神盾舰发 牢骚的积分是了绝色赌妃洛杉矶神盾舰付款了的数据神盾舰发了啥飞 机上洛杉矶的发了设计费是束带结发洛杉矶数据的发了急死了飞机就算了 的飞机上多了几分送洛杉矶的飞机上飞机束带结发洛杉矶发神经分裂的设计 费司法局束带结发洛杉矶发神经的发了几 随碟附送的积分松岛枫了所涉及的雷锋精神了</status> <address>beijing</address> <money>8000</money> </worker> <worker id = "A002"> <name>lucy</name> <sex>femalle</sex> <status>progremmar</status> <address>tianjin</address> <money>4000</money> </worker> </workers>
第一步:下载即将解析的xml文档,得到一个InputStream的输入流。
public static InputStream downloader(String path) { InputStream inputStream = null; try { URL url = new URL(path); if (url != null) { HttpURLConnection connection = (HttpURLConnection) url .openConnection(); // connection.setConnectTimeout(3000); // connection.setDoInput(true); // connection.setRequestMethod("GET"); inputStream = connection.getInputStream(); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return inputStream; }
第二步:获取SAXParserFactory对象
//得到输入流
inputStream = HttpDownloader .downloader("http://192.168.1.110:8080/mp3/worker.xml"); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream)); String line = ""; try {
//使用循环方法将输入流转换成文本字符 while ((line = bufferedReader.readLine()) != null) { xmlString.append(line); } //定义SAXParserFacotroy SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader reader = spf.newSAXParser().getXMLReader(); reader.setContentHandler(new SaxTools()); // 使用parse开始解析xml文件 reader.parse(new InputSource(new StringReader(xmlString .toString()))); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }
第三步:实现解析过程
package com.example.saxtools; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import android.widget.Toast; public class SaxTools extends DefaultHandler { private String hisname, address, money, sex, status; private String currentTagName; private StringBuilder sb = null; public SaxTools() { // TODO Auto-generated constructor stub } @Override public void startDocument() throws SAXException { // TODO Auto-generated method stub } @Override // 参数列表uri命名空间 // localName 不带前缀 // qName 带前缀 // attribute 标签中的属性 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { currentTagName = localName; if (localName.equals("worker")) { // 获取标签的全部属性 for (int i = 0; i < attributes.getLength(); i++) { // 将属性值输出 System.out.println(attributes.getLocalName(i) + "=" + attributes.getValue(i)); } } } @Override // 备注:char[]数组的默认长度是2024,如果读取的文本超过了2024个字节,characters函数将会执行第二次调用,第二次调用的时候, // char[]的长度依然是2024个字节,依次类推 public void characters(char[] ch, int start, int length) throws SAXException { // 实例化一个StringBuilder对象,用于存放characters获取到的数据 sb = new StringBuilder(); // 将获取到的数据添加到sb的末尾处 sb.append(ch, start, length); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub currentTagName = localName;// 当前解析的标签 if (currentTagName.equals("name")) { hisname = sb.toString(); } else if (currentTagName.equals("sex")) { sex = sb.toString(); } else if (currentTagName.equals("status")) { status = sb.toString(); } else if (currentTagName.equals("address")) { address = sb.toString(); } else if (currentTagName.equals("money")) { money = sb.toString(); } // 当完成一个对象的解析后,将这个对象的数据打印出来 if (currentTagName.equals("worker")) { this.printout(); } } @Override public void endDocument() throws SAXException { // TODO Auto-generated method stub } // 打印数据 public void printout() { System.out.println("name:" + hisname); System.out.println("sex:" + sex); System.out.println("address:" + address); System.out.println("status:" + status); System.out.println("money:" + money); } }
第四部:测试效果