Android学习笔记-XML处理
SAX工作原理:当扫描到文档(document)开始与结束、元素(element)开始与结束、文档(document)结束等地方时同志时间处理函数,由时间处理函数做相应动作,然后继续同样的扫描,直至文档结束。
从网络上下载XML代码进行处理
读取网络文件内容的方法参考 http://shamrock.blog.51cto.com/2079212/1580269
test.xml
<?xml version="1.0" encoding="UTF-8"?> <workers> <worker id="A01"> <name>Tom</name> <sex>男</sex> <status>经理</status> <address>湖北</address> <money>20000</money> </worker> <worker id="A02"> <name>Tom1</name> <sex>男</sex> <status>员工</status> <address>湖北武汉</address> <money>22000</money> </worker> <worker id="A03"> <name>Tom2</name> <sex>男</sex> <status>员工</status> <address>湖北宜昌</address> <money>20200</money> </worker> </workers>
下载文件不能在主线程中进行。
class MyHandler extends Handler { public MyHandler() { } public MyHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); Bundle bundle = msg.getData(); String urlStr = bundle.getString("urlStr"); String fileName = bundle.getString("fileName"); String path = bundle.getString("path"); HttpDownloader httpDownloader = new HttpDownloader(); //将读取到的XML文件的内容保存到result中 String result = httpDownloader.download(urlStr); System.err.println(result); Toast.makeText(MainActivity.this, "~~", Toast.LENGTH_SHORT).show(); try { // 创建一个SAXParserFactory SAXParserFactory factory = SAXParserFactory.newInstance(); XMLReader reader = factory.newSAXParser().getXMLReader(); // 为XMLReader设置内容处理器 reader.setContentHandler(new MyContentHandler()); // 开始解析文件 reader.parse(new InputSource(new StringReader(result))); } catch (Exception e) { e.printStackTrace(); } } }
MyContentHandler.java
package com.example.xml; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class MyContentHandler extends DefaultHandler { String hisname, address, money, sex, status; String tagName; public void startDocument() throws SAXException { System.out.println("````````begin````````"); } public void endDocument() throws SAXException { System.out.println("````````end````````"); } public void startElement(String namespaceURI, String localName, String qName, Attributes attr) throws SAXException { tagName = localName; if (localName.equals("worker")) { //获取标签的全部属性 for (int i = 0; i < attr.getLength(); i++) { System.out.println(attr.getLocalName(i) + "=" + attr.getValue(i)); } } } public void endElement(String namespaceURI, String localName, String qName) throws SAXException { //在workr标签解析完之后,会打印出所有得到的数据 tagName = ""; if (localName.equals("worker")) { this.printout(); } } public void characters(char[] ch, int start, int length) throws SAXException { if (tagName.equals("name")) hisname = new String(ch, start, length); else if (tagName.equals("sex")) sex = new String(ch, start, length); else if (tagName.equals("status")) status = new String(ch, start, length); else if (tagName.equals("address")) address = new String(ch, start, length); else if (tagName.equals("money")) money = new String(ch, start, length); } private void printout() { System.out.print("name: "); System.out.println(hisname); System.out.print("sex: "); System.out.println(sex); System.out.print("status: "); System.out.println(status); System.out.print("address: "); System.out.println(address); System.out.print("money: "); System.out.println(money); System.out.println(); } }
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1582142
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。