1、JAVABean
package com.wynlink.domain;
public class Person {
private Integer id;
private String name;
private Short age;
public Person() {
super();
}
public Person(String name, Short age) {
super();
this.name = name;
this.age = age;
}
public Person(Integer id, String name, Short age) {
super();
this.id = id;
this.name = name;
this.age = 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;
}
//重写toString方法
@Override
public String toString() {
return "id="+id+",name="+name+",age="+age;
}
}
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import android.test.AndroidTestCase;
import android.util.Log;
import com.wynlink.domain.Person;
import com.wynlink.service.DOMPersonService;
import com.wynlink.service.PullPersonService;
import com.wynlink.service.SAXPersonService;
/**
* JUnit测试类
* @author yaodaqing
*
*/
public class XMLPersonServiceTest extends AndroidTestCase {
public static final String TAG = "XMLPersonServiceTest";
/**
* JUnit测试
* 使用SAX方法解析XML内容
*/
public void testSAXReadXML() throws Exception{
InputStream inStream = XMLPersonServiceTest.class.getClassLoader().getResourceAsStream("wynlink.xml");//获取src目录下的文件
List<Person> persons = SAXPersonService.readxml(inStream);
for(Person person : persons){
Log.i(TAG, person.toString());
}
}
}
3、
package com.wynlink.service;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import com.wynlink.domain.Person;
/**
* SAX方式解析XML文件
* @author yaodaqing
*/
public class SAXPersonService {
/**
* 读取的文件以输入流的方式传进来
* 业务层不对异常进行处理,抛出给action进行处理
*/
public static List<Person> readxml(InputStream inStream) throws Exception {
SAXParserFactory saxpf = SAXParserFactory.newInstance();
SAXParser saxParser = saxpf.newSAXParser(); // 创建解析器
// 设置解析器的相关特性,http://xml.org/sax/features/namespaces = true
// 表示开启命名空间特性
//saxParser.setProperty("http://xml.org/sax/features/namespaces", true);
XMLContentHandler handler = new XMLContentHandler();
saxParser.parse(inStream, handler);//把文件流、解析方式传给SAX方式即可
inStream.close();
return handler.getPersons();
}
}
4、
package com.wynlink.service;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.wynlink.domain.Person;
/**
* SAX方式解析XML文件
* 解析器解析到哪个方法就调用哪个方法
* @author yaodaqing
*/
public class XMLContentHandler extends DefaultHandler {
private List<Person> persons;
private Person person;
private String preTag;
/**
* 专门做数据初始化方法
*/
@Override
public void startDocument() throws SAXException {
persons = new ArrayList<Person>();
}
/**
* 处理元素的text类型的节点
*/
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
/*if("name".equals(preTag)){
if(person!=null){
String name = new String(ch,start,length);
person.setName(name);
}
} else if("age".equals(preTag)){
if(person!=null){
String age = new String(ch,start,length);
person.setAge(new Short(age));
}
}*/
//优化方法如下
if(person!=null){
String data = new String(ch,start,length);//去除text文本标签;得到具体的标签中的值
if("name".equals(preTag)){
person.setName(data);//data中的内容就是节点"<name>李明</name>"中的内容;李明
} else if("age".equals(preTag)){
person.setAge(new Short(data));//data中的内容就是节点"<age>30</age>"中的内容;30
}
}
}
/**
* 执行元素的开始方法
* uri:命名空间
* localName:节点元素
* qName:节点元素的前缀
* attributes:节点后面属性
*/
@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("id")));
}
preTag = localName;//把值赋给一个变量
}
/**
* 执行元素的结束方法
*/
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if("person".equals(localName) && person!=null){//等于person节点时,表示已经到标签结尾,并且不为空;把对象加入到list中
persons.add(person);
person = null;
}
preTag = null;//把对象加入后,一定要清空
}
/**
* 给外界提供一个获取list的对象
* @return
*/
public List<Person> getPersons() {
return persons;
}
}
5、XML文件
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="23">
<name>大庆</name>
<age>30</age>
</person>
<person id="20">
<name>daqing</name>
<age>25</age>
</person>
</persons>
在android平台已经测试过。