package com;
import java.io.FileInputStream;
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;
/**
* SAX解析XML,事件驱动
* 只有两种节点
* Element Node元素节点
* Text Node文本节点
*/
public class SaxResolveXML {
public static void main(String[] args){
try {
SaxResolveXML saxResolveXML = new SaxResolveXML();
InputStream inStream = new FileInputStream("D:\\xml.xml");
List<Person> list = saxResolveXML.getList(inStream);
for(Person person : list){
System.out.println(person.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public List<Person> getList(InputStream inStream) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parse = factory.newSAXParser();
SaxResolve saxResolve = new SaxResolve();
parse.parse(inStream, saxResolve);
inStream.close();
return saxResolve.getPerson();
}
private final class SaxResolve extends DefaultHandler {
private List<Person> list = null;
private Person person = null;
private String tag = null;
public List<Person> getPerson(){
return list;
}
//开始文档事件
public void startDocument() throws SAXException {
//初始化
list = new ArrayList<Person>();
}
//开始元素语法事件 参数说明:命名空间、不带命名空间的标签名、含有命名空间前缀的标签名、属性
public void startElement(String uri, String localName, String qName,
Attributes atts) throws SAXException {
if("person".equals(qName)){
person = new Person();
person.setId(Integer.parseInt(atts.getValue(0)));
}
tag = qName;
}
//触发文本节点事件 参数说明:整个xml内容的字符串、当前读到文本类型的开始位置、当前读到文本的数据长度
public void characters(char[] ch, int start, int length)
throws SAXException {
if(tag != null){
String data = new String(ch, start, length);
if(tag.equals("name")){
person.setName(data);
}else if(tag.equals("age")){
person.setAge(Integer.parseInt(data));
}
}
}
//结束元素语法事件 参数说明:命名空间、不带命名空间的标签名、含有命名空间前缀的标签名
public void endElement(String uri, String localName, String qName)
throws SAXException {
if("person".equals(qName)){
list.add(person);
person = null;
//对象设为空
}
tag = null;
}
}
/*//开始文档事件
public void startDocument() throws SAXException {
}
//开始元素语法事件 参数说明:命名空间、不带命名空间的标签名、含有命名空间前缀的标签名、属性
public void startElement(String uri, String localName, String qName,
Attributes atts) throws SAXException {
}
//触发文本节点事件 参数说明:整个xml内容的字符串、当前读到文本类型的开始位置、当前读到文本的数据长度
public void characters(char[] ch, int start, int length)
throws SAXException {
}
//结束元素语法事件 参数说明:命名空间、不带命名空间的标签名、含有命名空间前缀的标签名
public void endElement(String uri, String localName, String qName)
throws SAXException {
}
public void endDocument() throws SAXException {
}
public void endPrefixMapping(String prefix) throws SAXException {
}
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
}
public void processingInstruction(String target, String data)
throws SAXException {
}
public void setDocumentLocator(Locator locator) {
}
public void skippedEntity(String name) throws SAXException {
}
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
}*/
}
xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="1">
<name>liming</name>
<age>23</age>
</person>
<person id="2">
<name>lixiangmei</name>
<age>24</age>
</person>
</persons>