SAX解析xml文件
一、student.xml(解析的文件)
<?xml version="1.0" encoding="utf-8" ?><!--xml文档声明-->
<!--DTD约束-->
<!DOCTYPE class [
<!ELEMENT student (name,age,sex,height)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT sex (#PCDATA)>
<!ELEMENT height (#PCDATA)>
]>
<!--只能有一个根标签-->
<class>
<!--设置属性-->
<student id="1" user="lisi">
<name>李四</name>
<age>18</age>
<sex>男</sex>
<height>182</height>
</student>
<student id="2" user="wangwu">
<name>王五</name>
<age>19</age>
<sex>男</sex>
<height>172</height>
</student>
</class>
二、解析代码(SAXParseMain.java)
package com.xml.main;
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.util.ArrayList;
public class SAXParseMain {
public static void main(String[] args) {
try {
//步骤一:创建SAX解析工厂实例
SAXParserFactory factory = SAXParserFactory.newInstance();
//步骤二:通过工厂创建一个SAX解析器
SAXParser sax = factory.newSAXParser();
MyHandler myHandler = new MyHandler();
//步骤三:指定用相应的解析器开始解析XML文档
sax.parse("student.xml", myHandler);
ArrayList<Student> list = myHandler.getList();
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.println(student.toString());
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}
//指定解析处理器,编写解析规则
class MyHandler extends DefaultHandler {
ArrayList<Student> list = new ArrayList<>();
public ArrayList<Student> getList() {
return list;
}
private Student student;
private String target = "";
// 解析到文档的开始
@Override
public void startDocument() throws SAXException {
}
// 解析到开始的元素节点(包括根节点)
/**
* @param uri 命名空间
* @param localName 元素名称【不支持了】
* @param qName [前缀:]元素名称(没有命名空间就不存在前缀)
* @param attributes 属性集
* @throws SAXException 异常
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if ("student".equals(qName)) {
student = new Student();
String id = attributes.getValue("id");
String user = attributes.getValue("user");
student.setId(Integer.parseInt(id));
student.setUser(user);
}
target = qName;
}
/**
* 解析到文本内容(包括回车,空格,换行)
*
* @param ch SAX并不是把整个文档加载到内存中,SAX会把解析到的内容缓存起来(缓存8192字节),char[] ch就是解析到的缓存内容
* @param start 遇到文本的开始字符位置
* @param length 当前整个文本从一个标签到下一个标签的长度
* @throws SAXException
*/
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String content = new String(ch, start, length);
switch (target) {
case "name":
student.setName(content);
break;
case "age":
student.setAge(Integer.parseInt(content));
break;
case "sex":
student.setSex(content);
break;
case "height":
student.setHeight(Double.parseDouble(content));
break;
}
}
/**
* 解析到结束的元素节点
*
* @param uri
* @param localName
* @param qName
* @throws SAXException
*/
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if ("student".equals(qName)) {
list.add(student);
}
target = "";
}
/**
* 解析到文档的结束
*
* @throws SAXException
*/
@Override
public void endDocument() throws SAXException {
}
}
三、Student.java
package com.xml.main;
public class Student {
private int id;
private String user;
private String name;
private String sex;
private int age;
private double height;
public Student() {
}
public Student(int id, String user, String name, String sex, int age, double height) {
this.id = id;
this.user = user;
this.name = name;
this.sex = sex;
this.age = age;
this.height = height;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", user='" + user + '\'' +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", height=" + height +
'}';
}
}
四、解析结果
注:读边解析,应用于大型XML文档