Java解析xml之SAX,Java,解析,xml,SAX
在上一节介绍了java用DOM的方式解析XML,本文用的到test.xml,请参考
上节。
-------------------------------------------
1.Java解析xml之DOM
--------------------------------------------------------------------------
SAX不用将整个文档加载到内存,基于事件驱动的API(Observer模式),用户只需要注册自己感兴趣的事件即可。SAX提供EntityResolver, DTDHandler, ContentHandler, ErrorHandler接口,分别用于监听解析实体事件、DTD处理事件、正文处理事件和处理出错事件,与AWT类似,SAX还提供了一个对这4个接口默认的类DefaultHandler(这里的默认实现,其实就是一个空方法),一般只要继承DefaultHandler,重写自己感兴趣的事件即可。
1package test.xml;
2
3import java.io.IOException;
4import java.io.InputStream;
5
6import javax.xml.parsers.ParserConfigurationException;
7import javax.xml.parsers.SAXParser;
8import javax.xml.parsers.SAXParserFactory;
9
10import org.xml.sax.Attributes;
11import org.xml.sax.InputSource;
12import org.xml.sax.Locator;
13import org.xml.sax.SAXException;
14import org.xml.sax.SAXParseException;
15import org.xml.sax.helpers.DefaultHandler;
16
17/** *//**
18 *
19 * @author whwang
20 */
21public class TestSAX {
22
23 public static void main(String[] args) {
24 read();
25 write();
26 }
27
28 public static void read() {
29 try {
30 SAXParserFactory factory = SAXParserFactory.newInstance();
31 SAXParser parser = factory.newSAXParser();
32 InputStream in = TestSAX.class.getClassLoader().getResourceAsStream("test.xml");
33 parser.parse(in, new MyHandler());
34 } catch (ParserConfigurationException e) {
35 e.printStackTrace();
36 } catch (SAXException e) {
37 e.printStackTrace();
38 } catch (IOException e) {
39 e.printStackTrace();
40 }
41 }
42
43 public static void write() {
44 System.err.println("纯SAX对于写操作无能为力");
45 }
46
47}
48
49// 重写对自己感兴趣的事件处理方法
50class MyHandler extends DefaultHandler {
51
52 @Override
53 public InputSource resolveEntity(String publicId, String systemId)
54 throws IOException, SAXException {
55 return super.resolveEntity(publicId, systemId);
56 }
57
58 @Override
59 public void notationDecl(String name, String publicId, String systemId)
60 throws SAXException {
61 super.notationDecl(name, publicId, systemId);
62 }
63
64 @Override
65 public void unparsedEntityDecl(String name, String publicId,
66 String systemId, String notationName) throws SAXException {
67 super.unparsedEntityDecl(name, publicId, systemId, notationName);
68 }
69
70 @Override
71 public void setDocumentLocator(Locator locator) {
72 super.setDocumentLocator(locator);
73 }
74
75 @Override
76 public void startDocument() throws SAXException {
77 System.err.println("开始解析文档");
78 }
79
80 @Override
81 public void endDocument() throws SAXException {
82 System.err.println("解析结束");
83 }
84
85 @Override
86 public void startPrefixMapping(String prefix, String uri)
87 throws SAXException {
88 super.startPrefixMapping(prefix, uri);
89 }
90
91 @Override
92 public void endPrefixMapping(String prefix) throws SAXException {
93 super.endPrefixMapping(prefix);
94 }
95
96 @Override
97 public void startElement(String uri, String localName, String qName,
98 Attributes attributes) throws SAXException {
99 System.err.print("Element: " + qName + ", attr: ");
100 print(attributes);
101 }
102
103 @Override
104 public void endElement(String uri, String localName, String qName)
105 throws SAXException {
106 super.endElement(uri, localName, qName);
107 }
108
109 @Override
110 public void characters(char[] ch, int start, int length)
111 throws SAXException {
112 super.characters(ch, start, length);
113 }
114
115 @Override
116 public void ignorableWhitespace(char[] ch, int start, int length)
117 throws SAXException {
118 super.ignorableWhitespace(ch, start, length);
119 }
120
121 @Override
122 public void processingInstruction(String target, String data)
123 throws SAXException {
124 super.processingInstruction(target, data);
125 }
126
127 @Override
128 public void skippedEntity(String name) throws SAXException {
129 super.skippedEntity(name);
130 }
131
132 @Override
133 public void warning(SAXParseException e) throws SAXException {
134 super.warning(e);
135 }
136
137 @Override
138 public void error(SAXParseException e) throws SAXException {
139 super.error(e);
140 }
141
142 @Override
143 public void fatalError(SAXParseException e) throws SAXException {
144 super.fatalError(e);
145 }
146
147 private void print(Attributes attrs) {
148 if (attrs == null) return;
149 System.err.print("[");
150 for (int i = 0; i < attrs.getLength(); i++) {
151 System.err.print(attrs.getQName(i) + " = " + attrs.getValue(i));
152 if (i != attrs.getLength() - 1) {
153 System.err.print(", ");
154 }
155 }
156 System.err.println("]");
157 }
158}
159