SAX解析XML
SAX是一个用于处理XML事件驱动的“推”模型,虽然它不是W3C标准,但它却是一个得到了广泛认可的API。SAX解析器不像DOM那样建立一个完整的文档树,而是在读取文档时激活一系列事件,这些事件被推给事件处理器,然后由事件处理器提供对文档内容的访问。
SAL解析xml文件步骤
1. 创建SAX解析器工厂对象
2. 通过解析器工厂对象获取解析器对象
3. 通过解析器对象获取reader读取器
4. 设置reader事件处理器
5. 利用reader读取xml文件
下面是用SAX解析studeng.xml文件事例代码:
package SAX;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public class SAXDemo02 {
/** * 使用Sax方法解析students.xml文件,保存到集合中 * @param args */
public static void main(String[] args) throws Exception{
//1.创建工厂 SAX分析工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
//2.创建解释器
SAXParser parser = factory.newSAXParser();
//3.从解释器获取读取器
XMLReader reader = parser.getXMLReader();
StudentContentHandler2 studentContentHandler2 = new StudentContentHandler2();
//4.设置内容处理器
reader.setContentHandler(studentContentHandler2);
//5.解析
reader.parse("xml\\student.xml");
for(Student s:studentContentHandler2.getList()){
System.out.println(s.getId()+"--"+s.getName()+"--"+s.getAge()+"--"+s.getSex());
}
}
}
class StudentContentHandler2 extends DefaultHandler{
ArrayList<Student> list =new ArrayList<Student>();
String currentTagName;
Student student = null;
public ArrayList<Student> getList() {
return list;
}
public void setList(ArrayList<Student> list) {
this.list = list;
}
public String getCurrentTagName() {
return currentTagName;
}
public void setCurrentTagName(String currentTagName) {
this.currentTagName = currentTagName;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
//内容处理器开始标签
@Override
public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException {
currentTagName = qName;
if(qName.equals("student")){
student = new Student();
}
}
//内容处理器 结束标签
@Override
public void endElement(String uri,String qName)
throws SAXException {
currentTagName = "";
if(qName.equals("student")){
list.add(student);
}
}
//内容处理 处理内容标签
@Override
public void characters(char[] ch,int start,int length)
throws SAXException {
if(currentTagName.equals("name")){
student.setName(new String(ch,start,length));
}else if(currentTagName.equals("age")){
student.setAge(Integer.parseInt(new String(ch,length)));
}else if(currentTagName.equals("sex")){
student.setSex(new String(ch,length));
}else if(currentTagName.equals("id")){
student.setId(Integer.parseInt(new String(ch,length)));
}
}
}
class Student{
private int id;
private String name;
private int age;
private String sex;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int id,String name,int age,String sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
studeng.xml文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student>
<id>1</id>
<name>zhangsan</name>
<age>20</age>
<sex>male</sex>
</student>
<student>
<id>2</id>
<name>lisi</name>
<age>21</age>
<sex>female</sex>
</student>
</students>