在Android中有几种xml解析方式,下面我说其中一种,在Android平台上可以使用Simple API for XML(SAX):
我的资源文件:value.xml
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<Person
Module="1">
<name>王二</name>
<age>18</age>
</Person>
<Person
Module="2">
<name>张三</name>
<age>20</age>
</Person>
<Person
Module="3">
<name>李四</name>
<age>22</age>
</Person>
</xml>
我的程序:
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class Sax extends DefaultHandler {
private String tag ;//标签
private String tag_value;//存放临时标签
private String name;
private String age;
/**
* 得到到标签元素值
* Packeage Module="fireware", Module 为属性 ,"fireware"为属性值。
<Version>smart.01.sc</Version> Version 为标签元素,smart.01.sc为元素值。
*/
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
// TODO Auto-generated method stub
if (tag != null) {
tag_value = new String(ch, start, length);
if (tag.equals("name")) {
name = tag_value;
System.out.println(" name = " + name);
}
if (tag.equals("age")) {
age = tag_value;
System.out.println(" age = " + age);
}
}
}
/**
* 解析结束执行一次
*/
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
/**
* 遇到结束标签就会执行
*/
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
// TODO Auto-generated method stub
tag = null;
}
/**
* 开始解析时调用一次
*/
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}
/**
* 遇到开始标签就会执行
* Attributes 中存放着标签属性
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
// TODO Auto-generated method stub
tag = qName;
for (int i = 0; i < attributes.getLength(); i++) {
System.out.println(" attributes.getValue(" + i + ") = " + attributes.getValue(i));
}
}
/**
* @param fileName
*/
public void parserXml(String fileName) {
InputStream is = null ;//创建输入流读入XML文件
try {
//创建解析器
SAXParserFactory saxfac = SAXParserFactory.newInstance();
SAXParser saxparser = saxfac.newSAXParser();
saxparser.setProperty("http://xml.org/sax/features/namespaces", true);//设置命名空间
System.out.println(fileName);
is = new FileInputStream(fileName);
//解析
saxparser.parse(is, new Sax());
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String [] str){
new Sax().parserXml("res/value.xml");
}
}
执行后打印:
res/value.xml
attributes.getValue(0) = 1
name = 王二
age = 18
attributes.getValue(0) = 2
name = 张三
age = 20
attributes.getValue(0) = 3
name = 李四
age = 22