<?xml version="1.0"?>
<!-- Java and XML Contents -->
<book>
<title>Java and XML</title>
<!-- Chapter List -->
<contents>
<chapter title="Introduction" number="1">
<topic name="XML Matters"/>
<topic name="What's Important"/>
<topic name="The Essentials"/>
<topic name="What's Next?"/>
</chapter>
<chapter title="Nuts and Bolts" number="2">
<topic name="The Basics"/>
<topic name="Constraints"/>
<topic name="Transformations"/>
<topic name="And More"/>
<topic name="What's Next?"/>
</chapter>
<chapter title="SAX" number="3">
<topic name="Getting Prepared"/>
<topic name="SAX Readers"/>
<topic name="Content Handlers"/>
<topic name="Gotcha!"/>
<topic name="What's Next?"/>
</chapter>
</contents>
</book>
java文件
package com.test;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class SaxXML extends DefaultHandler {
private int index1 = 0;
private int index2 = 0;
public void characters(char[] ch, int start, int length)
throws SAXException {
try {
String str = new String(ch, start, length);
if (str.trim().length() > 0) {
System.out.println("本书<<" + str + ">>的目录");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void endDocument() throws SAXException {
System.out.println("解析结束:");
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("chapter"))
this.index2 = 0;
}
public void startDocument() throws SAXException {
System.out.println("解析开始: ");
}
public void startElement(String uri, String localName, String qName,
Attributes atts) throws SAXException {
if (qName.equalsIgnoreCase("chapter")) {
index1++;
for (int i = 0; i < atts.getLength(); i++) {
String attName = atts.getQName(i);
if (attName.equalsIgnoreCase("title")) {
System.out.println("第" + index1 + "章:" + atts.getValue(i));
}
}
}
if (qName.equalsIgnoreCase("topic")) {
index2++;
for (int i = 0; i < atts.getLength(); i++) {
String attName = atts.getQName(i);
if (attName.equalsIgnoreCase("name")) {
System.out.println(" 第" + index2 + "部分:"
+ atts.getValue(i));
}
}
}
}
public static void main(String[] args) throws Exception {
String xmlURI = "contents.xml"; //相对路径
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(new SaxXML());
InputSource inputSource = new InputSource(xmlURI);
reader.parse(inputSource);
/* 第二种读取方法
SAXParserFactory factory=SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
SAXParser parser=factory.newSAXParser();
parser.parse("contents.xml", new SaxXML());*/
}
}
>>结果:
解析开始:
本书<<Java and XML>>的目录
第1章:Introduction
第1部分:XML Matters
第2部分:What's Important
第3部分:The Essentials
第4部分:What's Next?
第2章:Nuts and Bolts
第1部分:The Basics
第2部分:Constraints
第4部分:Transformations
第5部分:And More...
第6部分:What's Next?
第3章:SAX
第1部分:Getting Prepared
第2部分:SAX Readers
第3部分:Content Handlers
第4部分:Gotcha!
第5部分:What's Next?
解析结束: