Java解析xml文档之SAX解析

时间:2022-04-25 08:45:26

sax解析是一种边读边解析,仅向前读取,不能修改,用来读。

sax创建XMLReader三步:

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();

sax是使用类似于监听机制和适配器思想来做解析的,里面去覆盖方法:

reader.setContentHandler(new DefaultHandler(){}

具体实例代码如下:

sxaDemo

package cn.hncu.sax_stax;

import java.awt.Button;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.junit.Test;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class SaxDemo {
@Test //了解Sax解析的生命周期(工作流程)
public void SaxDemo1() throws Exception{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader=parser.getXMLReader();
reader.setContentHandler( new DefaultHandler(){
@Override
public void startDocument() throws SAXException {
System.out.println("1 开始读取文档了....");
}
@Override
public void endDocument() throws SAXException {
System.out.println("10 文档读取结束了....");
}
@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes) throws SAXException {
System.out.println("一个元素开始....");
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("一个元素结束....");
}
});

reader.parse("./xml5/users.xml");
System.out.println("Over.....");
}

//用SAX解析xml的基本套路
@Test
public void SaxDemo2() throws Exception{
SAXParserFactory factory = SAXParserFactory.newInstance();//1
SAXParser parser = factory.newSAXParser();//2
XMLReader reader = parser.getXMLReader();//3
reader.setContentHandler(new DefaultHandler(){
//覆盖相应的事件处理方法---最核心的步骤※※※※
});//4
reader.parse("./xml5/users.xml");//5
}


/*
* 需求:输出xml文档的数据信息,格式如下:
* id:A001
name:Jack
age:25
------------
id:A002
name:张三
age:80
-----------
。。。。。
*/

private String elementName="@#";
@Test
public void SaxDemo3() throws Exception{
SAXParserFactory factory = SAXParserFactory.newInstance();//1
SAXParser parser = factory.newSAXParser();//2
XMLReader reader = parser.getXMLReader();//3
reader.setContentHandler(new DefaultHandler(){

@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes) throws SAXException {
if(qName.equals("user")){
String id = attributes.getValue("id");
System.out.println("id:"+id);
}else if(qName.equals("name") || qName.equals("age")){
elementName = qName;
}
}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(qName.equals("name") || qName.equals("age")){
elementName ="@#";
}else if(qName.equals("user")){
System.out.println("--------------");
}
}

//读取:xml文档中的“文本内容”---不包含属性和标签,但是含有空格和换行等空白符
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(!elementName.equals("@#"))
System.out.println(elementName+":"+new String(ch,start,length));
}

});//4
reader.parse("./xml5/users.xml");//5
}

}

users.xml

<?xml version="1.0" encoding="utf-8"?>

<users>
<user id="A001">
<name>jack</name>
<age>23</age>
</user>
<user id="b001">
<name>Mike</name>
<age>25</age>
</user>
</users>