<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="23">
<name>
李明
</name>
<age>30</age>
</person>
<person id="20">
<name>
李向梅
</name>
<age>25</age>
</person>
</persons>
解析
person.xml
触发的事件为:
读到的标签及内容
触发事件
{
文档开始
} startDocument()
<persons> startElement
(“”,
"persons", null, "{Attributes}")
"\n\t" characters("<persons>...</persons>", "12", "2")
<person> startElement
(“”,
"person", null, "{Attributes}")
"\n\t\t" characters("<persons>...</persons>", "31", "3")
<name> startElement
(“”,
"name", null, "{Attributes}")
"
李明
" characters("<persons>...</persons>", "40", "2")
</name> endElement("", "name", null)
"\n\t\t" characters("<persons>...</persons>", "50", "3")
<age> startElement
(“”,
"age", null, "{Attributes}")
"30" characters("<persons>...</persons>", "58", "2")
</age> endElement("", "age", null)
"\n\t" characters("<persons>...</persons>", "67", "2")
</person> endElement("", "person", null)
"\n\t" characters("<persons>...</persons>", "79", "2")
<person> startElement
(“”,
"person", null, "{Attributes}")
"\n\t\t" characters("<persons>...</persons>", "98", "3")
<name> startElement
(“”,
"name", null, "{Attributes}")
"
李向梅
" characters("<persons>...</persons>", "107", "3")
</name> endElement("", "name", null)
"\n\t\t" characters("<persons>...</persons>", "118", "3")
<age> startElement
(“”,
"age", null, "{Attributes}")
"25" characters("<persons>...</persons>", "126", "2")
</age> endElement("", "age", null)
"\n\t" characters("<persons>...</persons>", "135", "2")
</person> endElement("", "person", null)
"\n" characters("<persons>...</persons>", "147", "1")
</persons> endElement("", "persons", null)
{
文档结束
} endDocument()
1.创建Android工程(eclipse3.6):
Project name:XML
BuildTarget:Android2.2
Application name:xml解析器
Package name:com.changcheng.androidxml
Create Activity:XMLActivity
Min SDK Version:8
2.需要解析的XML文件:person.xml
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="11">
<name>maozhuxi</name>
<age>55</age>
</person>
<person id="22">
<name>tiananmeng</name>
<age>66</age>
</person>
</persons>
该文件存放于src源码目录。
3.XML文件对应的实体Person:
package com.jbridge.xml.domain;
public class Person {
private Integer id;
private String name;
private Short age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return age;
}
public void setAge(Short age) {
this.age = age;
}
@Override
public String toString() {
return "Person [age=" + age + ", id=" + id + ", name=" + name + "]";
}
public Person(Integer id, String name, Short age) {
this.id = id;
this.name = name;
this.age = age;
}
public Person(){}
}
4.Sax解析XML的事件处理类:
package com.jbridge.xml.service;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.jbridge.xml.domain.Person;
public class SAXPersonService {
public List<Person> getSAXPersons(InputStream inStream) throws Exception{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();//创建解析器
//设置解析器的相关特性,http://xml.org/sax/features/namespaces = true 表示开启命名空间特性,这个可以不写
//saxParser.setProperty("http://xml.org/sax/features/namespaces",true);
PersonHandler handler = new PersonHandler();
parser.parse(inStream, handler);
inStream.close();
return handler.getPersons();
}
private final class PersonHandler extends DefaultHandler{
private List<Person> persons = null;
private Person person = null;
private String tag = null;//当前解析的元素标签
public List<Person> getPersons() {
return persons;
}
/**
* 接收文档的开始的通知。
*/
@Override
public void startDocument() throws SAXException {
persons = new ArrayList<Person>();
}
/**
* 接收元素开始的通知。
* namespaceURI:元素的命名空间
* localName:元素的本地名称(不带前缀)
* qName:元素的限定名(带前缀)
* atts:元素的属性集合
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if("person".equals(localName)){
person = new Person();
person.setId(new Integer(attributes.getValue(0)));
}
tag = localName;
}
/**
* 接收字符数据的通知。
*/
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if(tag!=null){
String textdata = new String(ch, start, length);
if("name".equals(tag)){
person.setName(textdata);
}else if("age".equals(tag)){
person.setAge(new Short(textdata));
}
}
}
/**
* 接收文档的结尾的通知。
* uri:元素的命名空间
* localName:元素的本地名称(不带前缀)
* name:元素的限定名(带前缀)
*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if("person".equals(localName)){
persons.add(person);
person = null;
}
tag = null;
}
}
}
5.编写测试Sax解析XML的类(测试要配置AndroidManifest.xml)
package com.jbridge.xml.activity;
import java.io.InputStream;
import java.util.List;
import com.jbridge.xml.domain.Person;
import com.jbridge.xml.service.SAXPersonService;
import android.test.AndroidTestCase;
import android.util.Log;
public class TestService extends AndroidTestCase {
private static final String TAG = "PersonServiceTest";
public void testGetSAXPersons() throws Exception {
InputStream inputStream = TestService.class.getClassLoader()
.getResourceAsStream("person.xml");
SAXPersonService saxPersonService = new SAXPersonService();
List<Person> persons = saxPersonService.getSAXPersons(inputStream);
inputStream.close();
for (Person person : persons) {
Log.i(TAG, person.toString());
}
}
}
6.运行测试类
其他代码见附件。