您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
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(); } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。