如上圖,在觸發器中,首先開始讀取文檔,然後開始逐個解析元素,每個元素中的內容會返回到characters()方法,接著結束元素讀取,所有元素讀取完後,結束文檔解析。
現在我們開始創建觸發器這個類,要創建這個類首先需要繼承DefaultHandler
創建SaxHandler,並覆寫相應方法:
import
org.xml.sax.Attributes;
import
org.xml.sax.SAXException;
import
org.xml.sax.helpers.DefaultHandler;
public
class
SaxHandler
extends
DefaultHandler {
@Override
public
void
characters(
char
[] arg0,
int
arg1,
int
arg2)
throws
SAXException {
String content =
new
String(arg0, arg1, arg2);
System.out.println(content);
super
.characters(arg0, arg1, arg2);
}
@Override
public
void
endDocument()
throws
SAXException {
System.out.println(
"\n…………结束解析文档…………"
);
super
.endDocument();
}
@Override
public
void
endElement(String arg0, String arg1, String arg2)
throws
SAXException {
System.out.println(
"结束解析元素 "
+ arg2);
super
.endElement(arg0, arg1, arg2);
}
@Override
public
void
startDocument()
throws
SAXException {
System.out.println(
"…………开始解析文档…………\n"
);
super
.startDocument();
}
@Override
public
void
startElement(String arg0, String arg1, String arg2,
Attributes arg3)
throws
SAXException {
System.out.println(
"开始解析元素 "
+ arg2);
if
(arg3 !=
null
) {
for
(
int
i =
0
; i < arg3.getLength(); i++) {
System.out.print(arg3.getQName(i) +
"=\""
+ arg3.getValue(i) +
"\""
);
}
}
System.out.print(arg2 +
":"
);
super
.startElement(arg0, arg1, arg2, arg3);
}
}
XML文档:
<? xml version="1.0" encoding="UTF-8"?>
< books >
< book id="001">
< title >Harry Potter</ title >
< author >J K. Rowling</ author >
</ book >
< book id="002">
< title >Learning XML</ title >
< author >Erik T. Ray</ author >
</ book >
</ books >
|
TestDemo测试类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import java.io.File;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class TestDemo {
public static void main(String[] args) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
File f = new File( "books.xml" );
SaxHandler dh = new SaxHandler();
parser.parse(f, dh);
}
}
|
输出结果:
…………开始解析文档…………
开始解析元素 books
books:
开始解析元素 book
id="001"book:
开始解析元素 title
title:Harry Potter
结束解析元素 title
开始解析元素 author
author:J K. Rowling
结束解析元素 author
结束解析元素 book
开始解析元素 book
id="002"book:
开始解析元素 title
title:Learning XML
结束解析元素 title
开始解析元素 author
author:Erik T. Ray
结束解析元素 author
结束解析元素 book
结束解析元素 books
…………结束解析文档…………
|
上面的虽然正确显示了执行流程,但是输出却很乱
为了更加清晰的执行此流程,我们还可以重写SaxHandler,使其将原先的xml文档还原一遍
重写的SaxHandler类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SaxHandler extends DefaultHandler {
@Override
public void characters( char [] arg0, int arg1, int arg2) throws SAXException {
System.out.print( new String(arg0, arg1, arg2));
super .characters(arg0, arg1, arg2);
}
@Override
public void endDocument() throws SAXException {
System.out.println( "\n结束解析" );
super .endDocument();
}
@Override
public void endElement(String arg0, String arg1, String arg2)
throws SAXException {
System.out.print( "</" );
System.out.print(arg2);
System.out.print( ">" );
super .endElement(arg0, arg1, arg2);
}
@Override
public void startDocument() throws SAXException {
System.out.println( "开始解析" );
String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" ;
System.out.println(s);
super .startDocument();
}
@Override
public void startElement(String arg0, String arg1, String arg2,
Attributes arg3) throws SAXException {
System.out.print( "<" );
System.out.print(arg2);
if (arg3 != null ) {
for ( int i = 0 ; i < arg3.getLength(); i++) {
System.out.print( " " + arg3.getQName(i) + "=\"" + arg3.getValue(i) + "\"" );
}
}
System.out.print( ">" );
super .startElement(arg0, arg1, arg2, arg3);
}
}
|
执行结果:
From: 使用SAX解析 , 清新下午茶 , developerWorkers