xml解析--SAX解析
程序员文章站
2022-05-28 08:02:56
...
xml解析---SAX
工程结构图:
[img]
[img]http://dl.iteye.com/upload/attachment/0066/4386/26c1c362-c80b-37ab-92a9-2a45c38b9a0c.jpg[/img]
[/img]
这里的stu.xml文件是在src下的,而不是包里面的!
一、xml文件(随便写了一个)
二、Student类
三、MyDefaultHandler
四、测试:Test
工程结构图:
[img]
[img]http://dl.iteye.com/upload/attachment/0066/4386/26c1c362-c80b-37ab-92a9-2a45c38b9a0c.jpg[/img]
[/img]
这里的stu.xml文件是在src下的,而不是包里面的!
一、xml文件(随便写了一个)
<?xml version="1.0" encoding="utf-8"?>
<students>
<student id="1">
<name>tom</name>
<age>20</age>
</student>
<student id="2">
<name>kite</name>
<age>21</age>
</student>
<student id="3">
<name>rose</name>
<age>22</age>
</student>
</students>
二、Student类
package com.amaker.sax;
public class Student {
private int id;
private int age;
private String name;
/**
* 无参构造方法
*/
public Student() {
super();
}
/**
* 带参构造方法
*/
public Student(int id, int age, String name) {
super();
this.id = id;
this.age = age;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [age=" + age + ", id=" + id + ", name=" + name + "]";
}
}
三、MyDefaultHandler
package com.amaker.sax;
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 MyDefaultHandler extends DefaultHandler {
private List<Student> studentList;
private Student currentStudent;
private String currentTag;
@Override
public void startDocument() throws SAXException {
super.startDocument();
studentList = new ArrayList<Student>();
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if(qName!=null&&qName.equals("student")){
currentStudent = new Student();
int id = Integer.parseInt(attributes.getValue("id"));
currentStudent.setId(id);
}
currentTag = qName;
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
if(qName!=null&&qName.equals("student")){
studentList.add(currentStudent);
currentStudent = null;
}
currentTag = null;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
if(currentTag!=null){
String data = new String(ch,start,length);
if(currentTag.equals("name")){
currentStudent.setName(data);
}
if(currentTag.equals("age")){
currentStudent.setAge(Integer.parseInt(data));
}
}
}
public List<Student> getStudents(){
return studentList;
}
}
四、测试:Test
package com.amaker.sax;
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;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
parse();
}
public static void parse(){
InputStream in = new Test().getClass().getClassLoader().getResourceAsStream("stu.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parser= factory.newSAXParser();
MyDefaultHandler handler = new MyDefaultHandler();
parser.parse(in, handler);
List<Student> list = handler.getStudents();
for(Student s:list){
System.out.println(s);
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
上一篇: SAX解析 (解析XML文件)
下一篇: Java SAX 解析xml