import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
/**
*
* XML解析:
*
* SAX解析,基于事件的,按顺序解析
*
*
*/
public class XMLParserDemo {
public static void main(String[] args) {
try {
xmlSaxParser();
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
private static void xmlSaxParser() throws ParserConfigurationException,SAXException,IOException {
//1.获取一个sax解析器工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
//2.获取一个sax解析器
SAXParser saxParser = factory.newSAXParser() ;
PersonHandler personHandler = new PersonHandler();
InputStream f = Thread.currentThread().getContextClassLoader().getResourceAsStream("person.xml");
//3.开始解析
saxParser.parse(f,personHandler);
List<Person> ps = personHandler.getPersons();
for (Person person : ps) {
System.out.println(person);
}
}
}
********************************************************************************************************************
PersonHandler类
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 PersonHandler extends DefaultHandler {
private List<Person> persons = null;
private Person currentPerson = null;
//解析到文档的开始
@Override
public void startDocument() throws SAXException {
super.startDocument();
System.out.println("startDocument()");
//创建集合存储数据
persons = new ArrayList<Person>();//准备工作
}
/**
* uri:命名空间
* localName:标签名,不带前缀
* qName:标签名(带前缀)
* attributes:属性的集合
*/
String qName =null;
@Override
public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException {
super.startElement(uri,localName,qName,attributes);
System.out.println("startElement()");
if("person".equals(qName)){
currentPerson = new Person();//准备工作
currentPerson.setId(attributes.getValue("personid"));
}
this.qName =qName;
}
@Override
//注意:当读到一个标签的末尾时,会自动读后面的空格,直到下一行的开头,会覆盖之前读取的值,所以每次执行完characters()后,将qName置为空
public void characters(char[] ch,int start,int length) throws SAXException {
super.characters(ch,start,length);
System.out.println("characters()");
if("name".equals(qName)){
currentPerson.setName(new String(ch,length));
}else if("address".equals(qName)){
currentPerson.setAddress(new String(ch,length));
}else if("fax".equals(qName)){
currentPerson.setFax(new String(ch,length));
}else if("email".equals(qName)){
currentPerson.setEmail(new String(ch,length));
}
qName = null;
}
@Override
public void endElement(String uri,String qName) throws SAXException {
super.endElement(uri,qName);
System.out.println("endElement()");
if("person".equals(qName)){
persons.add(currentPerson);
}
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
System.out.println("endDocument()");
System.out.println("文档解析完成!!!");
}
public List<Person> getPersons() {
return persons;
}
}
***********************************************************************************************************************
Person类//存对应的数据
public class Person {
private String id;
private String name;
private String address;
private String tel;
private String fax;
private String email;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String id,String name,String address,String tel,String fax,String email) {
super();
this.id = id;
this.name = name;
this.address = address;
this.tel = tel;
this.fax = fax;
this.email = email;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getFax() {
return fax;
}
public void setFax(String fax) {
this.fax = fax;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Person [id=" + id + ",name=" + name + ",address=" + address + ",tel=" + tel + ",fax=" + fax
+ ",email=" + email + "]";
}
}
*********************************************************************************
person.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<people>
<person personid="E01">
<name>Tony Blair</name>
<address>10 Downing Street,London,UK</address>
<tel>(061) 98765</tel>
<fax>(061) 98765</fax>
<email>blair@everywhere.com</email>
</person>
<person personid="E02">
<name>Bill Clinton</name>
<address>White House,USA</address>
<tel>(001) 6400 98765</tel>
<fax>(001) 6400 98765</fax>
<email>bill@everywhere.com</email>
</person>
<person personid="E03">
<name>Tom Cruise</name>
<address>57 Jumbo Street,New York,USA</address>
<tel>(001) 4500 67859</tel>
<fax>(001) 4500 67859</fax>
<email>cruise@everywhere.com</email>
</person>
<person personid="E04">
<name>Linda Goodman</name>
<address>78 Crax Lane,UK</address>
<tel>(061) 54 56789</tel>
<fax>(061) 54 56789</fax>
<email>linda@everywhere.com</email>
</person>
</people>
******************************************************************************************************************
执行结果
startDocument()//一开始进入文档
startElement()//<people>标签
characters()//</people>与下一行<person>之前的空白
startElement()//第一个<person>标签
characters()//<person>与<name>之间的内容+空白
startElement() //<name>标签
characters() //<name>与</name>之间的文字
endElement() //</name>标签
characters() //</name>与<address>之间的空白,下面依次类推
startElement()
characters()
endElement()
characters()
startElement()
characters()
endElement()
characters()
startElement()
characters()
endElement()
characters()
startElement()
characters()
endElement()
characters() //</email>与下一行</person>之间的空白
endElement() //</person>标签
characters() //</person>与下一行<person>之间的空白
startElement() //第二个<person>标签 characters() startElement() characters() endElement() characters() startElement() characters() endElement() characters() startElement() characters() endElement() characters() startElement() characters() endElement() characters() startElement() characters() endElement() characters() endElement() characters() startElement() characters() startElement() characters() endElement() characters() startElement() characters() endElement() characters() startElement() characters() endElement() characters() startElement() characters() endElement() characters() startElement() characters() endElement() characters() endElement() characters() startElement() characters() startElement() characters() endElement() characters() startElement() characters() endElement() characters() startElement() characters() endElement() characters() startElement() characters() endElement() characters() startElement() characters() endElement() characters() endElement() characters() endElement() endDocument() Person [id=E01,name=Tony Blair,address=10 Downing Street,UK,tel=null,fax=(061) 98765,email=blair@everywhere.com] Person [id=E02,name=Bill Clinton,address=White House,USA,fax=(001) 6400 98765,email=bill@everywhere.com] Person [id=E03,name=Tom Cruise,address=57 Jumbo Street,fax=(001) 4500 67859,email=cruise@everywhere.com] Person [id=E04,name=Linda Goodman,address=78 Crax Lane,fax=(061) 54 56789,email=linda@everywhere.com]