android系统支持使用SAX、DOM或者pull读取xml文件(建议不要使用DOM解析-它比较耗内存)
SAX是一个解析速度快并且占用内存少的xml解析器。它解析的时候采用事件驱动。
即它不需要解析完整个文档,在按内容顺序解析文档的过程中,SAX会判断当前读到的字符是否合法的XML语法的某部分,
如果符合,则会调用某个回调方法——这些方法定义在ContentHandle接口中。
startDocument()
当遇到文档的开头的时候,调用该方法,可以做一些预处理的工作
endDocument()
当文档结束的时候,调用该方法,可以做一些善后工作
startElement(String namespaceURI,String localName,String qName,Attributes attr)
当读到一个开始标签的时候,会触发这个方法
参数分别为:命名空间、不带命名空间的标签名、带命名空间的标签名、得到的所有属性名和相应的值
endElement(String uri,String localName,String name)
在遇到结束标签的时候,调用该方法
characters(char[] ch,int start,int length)
这个方法来处理在xml文件中读到的内容,第一个参数用于存放文件的内容,后面的两个参数是读到的字符串
在这个数组中的起始位置和长度,使用new String(ch,start,length)就可以获取内容
如有以下xml文件
LiMing
XiaoZhang
解析过程触发的事件为:
读到的标签及内容 触发事件
{文档开始} startDocument()
startElement("","persons",null,"{Attributes}") "\n\t"
characters("
...","12","2")
startElement("","person",null,"{Attributes}") "\n\t\t"
characters("
...","31","3")
startElement("","name",null,"{Attributes}")
"LiMing"
characters("
...","40","2")
endElement("","name",null)
"\n\t\t"
characters("
...","50","3")
startElement("","age",null,"{Attributes}") "30"
characters("
...","58","2")
endElement("","age",null)
"\n\t"
characters("
...","67","2")
endElement("","person",null)
"\n\t"
characters("
...","79","2")
startElement("","person",null,"{Attributes}") "\n\t\t"
characters("
...","98","3")
startElement("","name",null,"{Attributes}")
"XiaoZhang"
characters("
...","107","3")
endElement("","name",null)
"\n\t\t"
characters("
...","118","3")
startElement("","age",null,"{Attributes}") "25"
characters("
...","126","2")
endElement("","age",null)
"\n\t\t"
characters("
...","135","2")
endElement("","person",null)
"\n"
characters("
...","147","1")
endElement
写了一个sax解析程序,来解析该xml文件
- import org.xml.sax.Attributes;
-
import org.xml.sax.SAXException;
-
import org.xml.sax.helpers.DefaultHandler;
-
-
public class StudentInfoContentHandler2 extends DefaultHandler {
-
-
@Override
-
public void characters(char[] ch, int start, int length) throws SAXException {
-
if(person!=null) {
-
String data = new String(ch, start, length);
-
if("name".equals(tagName)) {
-
person.name = data;
-
} else if("age".equals(tagName)) {
-
person.age = Integer.valueOf(data);
-
}
-
}
-
}
-
-
@Override
-
public void endDocument() throws SAXException {
-
for(Person p : personList) {
-
System.out.println(p);
-
}
-
}
-
-
@Override
-
public void endElement(String uri, String localName, String qName) throws SAXException {
-
if("person".equals(qName)) {
-
personList.add(person);
-
person = null;
-
}
-
tagName = null;
-
}
-
-
@Override
-
public void startDocument() throws SAXException {
-
personList = new ArrayList<Person>();
-
}
-
-
@Override
-
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
-
if("person".equals(qName)) {
-
person = new Person();
-
person.id = Integer.valueOf(attributes.getValue("id"));
-
} else {
-
-
}
-
tagName = qName;
-
}
-
-
List<Person> personList;
-
Person person;
-
String tagName;
-
}
-
-
-
class Person
-
{
-
@Override
-
public String toString() {
-
return "id=" + id + " name=" + name + " age=" + age;
-
}
-
public int id;
-
public String name;
-
public int age;
-
-
}
写了一个测试程序,来测试解析过程是否正确:
- import java.io.File;
-
import java.io.IOException;
-
-
import javax.xml.parsers.ParserConfigurationException;
-
import javax.xml.parsers.SAXParser;
-
import javax.xml.parsers.SAXParserFactory;
-
-
import org.xml.sax.SAXException;
-
-
-
public class SAX_XML_Parser {
-
- public static void main(String[] args) {
-
try {
-
readXml();
-
} catch (ParserConfigurationException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
} catch (SAXException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
} catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
-
private static void readXml() throws ParserConfigurationException, SAXException, IOException {
-
File file = new File("C:/studentInfo.xml");
-
SAXParserFactory spf = SAXParserFactory.newInstance();
-
SAXParser saxParser = spf.newSAXParser();
-
//saxParser.setProperty("", true);
-
StudentInfoContentHandler2 handler = new StudentInfoContentHandler2();
-
saxParser.parse(file, handler);
-
}
-
}
输出结果:
id=10 name=LiMing age=25
id=20 name=XiaoZhang age=30
阅读(2705) | 评论(0) | 转发(0) |