Sax解析xml文件
程序员文章站
2022-03-03 15:06:06
...
将所有文件放在包下即可运行。
解析过程:
1.获取解析工厂
2.从解析工厂中获得解析器
3.加载文档Document 编写处理器(重难点)
Students.xml
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student id="1">
<name>王同</name>
<course>java</course>
<score>89</score>
</student>
<student id="2">
<name>李佳</name>
<course>sql</course>
<score>58</score>
</student>
</students>
Student.java
package sax;
public class Student {
private String name;
private String course;
private int score;
private int id;
public Student() {
}
public Student(String name, String course, int score, int id) {
super();
this.name = name;
this.course = course;
this.score = score;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
parse.java
package sax;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class parse {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException{
//1.获取解析工厂
SAXParserFactory factory=SAXParserFactory.newInstance();
//2.从解析工厂获得解析器
SAXParser parse=factory.newSAXParser();
//3.加载文档Document 注册处理器
//4.编写处理器
StudentHandler handler=new StudentHandler();
//此写法针对并发操作,正常写xml路径即可
parse.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("sax/Student.xml"),handler);
System.out.println("\n");
List <Student> students = handler.getStudents();
for(Student s:students){
System.out.println(s.getName()+"-->"+s.getCourse()+"-->"+s.getScore());
}
}
}
StudentHandler.java
package sax;
/**
* 处理器
*/
import java.util.ArrayList;
import java.util.List;
import org.dom4j.tree.DefaultAttribute;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class StudentHandler extends DefaultHandler {
private List<Student> students;
private Student student;
private String tag; // 记录标签名
@Override
public void startDocument() throws SAXException {
students = new ArrayList<Student>();
}
// qName:标签名
// attributes:属性对象
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName != null) {
tag = qName;
}
if (qName != null && qName.equals("student")) {
student = new Student();
student.setId(Integer.parseInt(attributes.getValue("id")));
System.out.print("<" + qName + " " + attributes.getQName(0) + "=" + attributes.getValue("id") + ">");
} else {
System.out.print("<" + qName + ">");
}
}
// 文本信息
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String str = new String(ch, start, length);
if (null != tag && tag.equals("name")) {
student.setName(str);
} else if (null != tag && tag.equals("course")) {
student.setCourse(str);
} else if (null != tag && tag.equals("score")) {
Integer score = Integer.valueOf(str);
student.setScore(score);
}
System.out.print(str);
}
// 以“/”位结束标志
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
System.out.print("</" + qName + ">");
if (qName.equals("student")) {
this.students.add(student);
}
tag = null;
}
@Override
public void endDocument() throws SAXException {
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
上一篇: SAX解析XML文件
下一篇: 简单使用JDOM解析XML