java----SAX解析XML

时间:2024-08-02 20:33:26

XML:

可扩展标记语言

1、充当显示数据

2、储存数据

3、传输数据

SAX:

基于事假驱动,顺序读写,速度快

不能任意读取节点,灵活性差

解析时,占用内存小

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; public class Demo{
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
xmlTest(); }
public static void xmlTest() throws ParserConfigurationException, SAXException, IOException {
//创建一个SAX解析器工厂对象;
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
//通过工厂对象创建SAX解析器
SAXParser saxParser = saxParserFactory.newSAXParser();
//创建一个数据处理器(自己实现)
PersonHandle personHandle = new PersonHandle();
//开始解析
InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("person.xml");
saxParser.parse(resourceAsStream,personHandle);
List<Person> persons = personHandle.getPersons();
for(Person p:persons){
System.out.println(p+",p");
}
}
} class Person{
private String name;
private String age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} @Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
class PersonHandle extends DefaultHandler {
private List<Person> persons = null;
private String tag; //用来存储当前解析的标签名字
private Person person=null; public List<Person> getPersons() {
return persons;
} //开始解析文档时调用,只会执行一次
@Override
public void startDocument() throws SAXException {
super.startDocument();
persons = new ArrayList<>();
System.out.println("开始解析文档.....");
}
//结束解析文档时调用
@Override
public void endDocument() throws SAXException {
super.endDocument();
System.out.println("结束解析文档.....");
}
//每一个标签开始时调用 /**
*
* @param uri:命名空间
* @param localName :不带前缀的标签名 <a:person person_id="p1"> -->获取的是person
* @param qName:带前缀的标签名 <a:person person_id="p1"> -->获取的是a:person
* @param attributes:当前标签属性 <a:person person_id="p1">
* @throws SAXException
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
//获取每一个标签的person_id属性,如果没有返回null;
//System.out.println(attributes.getValue("person_id"));
if("person".equals(qName)){
person = new Person();
}
// if ("name".equals(qName)){
// tag=qName;
// }else if ("age".equals(qName)){
// tag=qName;
// }
tag = qName;
}
//每一个标签结束时调用
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
if ("person".equals(qName)){
persons.add(person);
}
tag=null;
}
//当解析到标签中的内容的时候调用(换行也是文本内容)
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
if(tag!=null){
if ("name".equals(tag)){
person.setName(new String(ch,start,length));
}
if ("age".equals(tag)){
person.setAge(new String(ch,start,length));
}
}
}
}

person.xml

<?xml version="1.0" encoding="UTF-8"?>
<persions>
<person>
<name>小明</name>
<age>18</age>
</person>
<person>
<name>小花</name>
<age>17</age>
</person>
</persions>