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,事件驅(qū)動(dòng)
* 只有兩種節(jié)點(diǎn)
* Element Node元素節(jié)點(diǎn)
* Text Node文本節(jié)點(diǎn)
*/
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>();
}
//開始元素語(yǔ)法事件 參數(shù)說(shuō)明:命名空間、不帶命名空間的標(biāo)簽名、含有命名空間前綴的標(biāo)簽名、屬性
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;
}
//觸發(fā)文本節(jié)點(diǎn)事件 參數(shù)說(shuō)明:整個(gè)xml內(nèi)容的字符串、當(dāng)前讀到文本類型的開始位置、當(dāng)前讀到文本的數(shù)據(jù)長(zhǎng)度
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));
}
}
}
//結(jié)束元素語(yǔ)法事件 參數(shù)說(shuō)明:命名空間、不帶命名空間的標(biāo)簽名、含有命名空間前綴的標(biāo)簽名
public void endElement(String uri, String localName, String qName)
throws SAXException {
if("person".equals(qName)){
list.add(person);
person = null;
//對(duì)象設(shè)為空
}
tag = null;
}
}
/*//開始文檔事件
public void startDocument() throws SAXException {
}
//開始元素語(yǔ)法事件 參數(shù)說(shuō)明:命名空間、不帶命名空間的標(biāo)簽名、含有命名空間前綴的標(biāo)簽名、屬性
public void startElement(String uri, String localName, String qName,
Attributes atts) throws SAXException {
}
//觸發(fā)文本節(jié)點(diǎn)事件 參數(shù)說(shuō)明:整個(gè)xml內(nèi)容的字符串、當(dāng)前讀到文本類型的開始位置、當(dāng)前讀到文本的數(shù)據(jù)長(zhǎng)度
public void characters(char[] ch, int start, int length)
throws SAXException {
}
//結(jié)束元素語(yǔ)法事件 參數(shù)說(shuō)明:命名空間、不帶命名空間的標(biāo)簽名、含有命名空間前綴的標(biāo)簽名
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>