Android学习笔记-XML处理
SAX工作原理:当扫描到文档(document)开始与结束、元素(element)开始与结束、文档(document)结束等地方时同志时间处理函数,由时间处理函数做相应动作,然后继续同样的扫描,直至文档结束。

云溪网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。创新互联公司从2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司。
从网络上下载XML代码进行处理
读取网络文件内容的方法参考 http://shamrock.blog.51cto.com/2079212/1580269
test.xml
Tom 男 经理 湖北20000 Tom1 男 员工 湖北武汉22000 Tom2 男 员工 湖北宜昌20200
下载文件不能在主线程中进行。
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();
}
}文章名称:Android学习笔记-XML处理
本文路径:http://www.jxjierui.cn/article/phodgi.html


咨询
建站咨询
