sax解析xml案例二
ljq.xml文件
<?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实体类
package com.ljq.service;
public class Person {
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;
}
}
XMLContentHandler
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;
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>();
}
@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)) {
person.setName(date);
}else if ("age".equals(preTag)) {
person.setAge(new Short(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 && "person".equals(localName)) {
persons.add(person);
person = null;
}
preTag = null;
}
public List<Person> getPersons() {
return persons;
}
}
SaxPersonService
package com.ljq.service;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import android.util.Log;
public class SaxPersonService {
// 业务层要对外抛出
public static List<Person> 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();
}
}
SaxPersonServiceTest
package com.ljq.service;
import java.io.InputStream;
import java.util.List;
import android.test.AndroidTestCase;
import android.util.Log;
public class SaxPersonServiceTest extends AndroidTestCase{
private final String TAG = "SaxPersonServiceTest";
public void testReadXML() throws Exception{
InputStream inputStream = SaxPersonServiceTest.class.getClassLoader().getResourceAsStream("ljq.xml");
List<Person> persons = SaxPersonService.readXML(inputStream);
for(Person person : persons){
Log.i(TAG, person.getId() + " : " + person.getName() + " : " + person.getAge());
}
}
}