import java.io.Serializable;
/**
* 实体类
*
* @author jiqinlin
*
*/
@SuppressWarnings("serial")
public class Person implements Serializable{
private Integer id;
private String name;
private Short age;
public Person() {
}
public Person(String name, Short age) {
super();
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;
}
}
handle类
代码如下:
package com.ljq.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.ljq.entity.Person;
public class XMLContentHandler extends DefaultHandler {
//临时变量,解决sax读取XML时不能读取换行符号后面的内容的问题;注意temp=""不能写成temp或temp=null,避免脏数据
private String temp="";
private String preTag;
private Person person;
private ListPerson persons;
@Override
public void startDocument() throws SAXException {
persons = new ArrayListPerson();
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (person!=null){
String date = new String(ch,start,length);
if ("name".equals(preTag)) {
temp+=date;
}else if ("age".equals(preTag)) {
temp+=date;
}
}
}
@Override
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
if ("person".equals(localName)) {
person= new Person();
person.setId(attributes.getIndex("id"));
}
preTag = localName;
}
@Override
public void endElement(String uri, String localName, String name) throws SAXException {
if (person !=null && "name".equals(localName)) {
person.setName(temp);
temp="";
}else if (person !=null && "age".equals(localName)) {
person.setAge(new Short(temp));
temp="";
}else if (person !=null && "person".equals(localName)) {
persons.add(person);
person = null;
}
preTag = null;
}
//getter...
public ListPerson getPersons() {
return persons;
}
}
代码如下:
package com.ljq.service;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import com.ljq.entity.Person;
/**
* sax解析工厂
*
* @author jiqinlin
*
*/
public class SaxPersonService {
// 业务层要对外抛出
public static ListPerson readXML(InputStream inStream) throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser saxParser = spf.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);
inStream.close();
return handler.getPersons();
}
}
测试类
代码如下:
package com.ljq.test;
import java.io.InputStream;
import java.util.List;
import com.ljq.entity.Person;
import com.ljq.service.SaxPersonService;
import android.test.AndroidTestCase;
import android.util.Log;
public class SaxPersonServiceTest extends AndroidTestCase{
private final String TAG = "SaxPersonServiceTest";
public void testReadXML() throws Exception{
//ljq.xml放在src目录下
InputStream inputStream = SaxPersonServiceTest.class.getClassLoader().getResourceAsStream("ljq.xml");
ListPerson persons = SaxPersonService.readXML(inputStream);
for(Person person : persons){
Log.i(TAG, person.getId() + " : " + person.getName() + " : " + person.getAge());
}
}
}
运行结果
在android模拟器中,如果是中文字符会出现乱码,在真机上就不会出现这种情况