I share my expercies in running
Step1: download Xerces-J-bin.2.11.0.zip from www.apache.org
Step2: You extract ZIP files .
Step3: run samples
run -> cmd
go to directory of Xerces-J-bin
command line:
(1)java -classpath xercesImpl.jar;xml-apis.jar;xercesSamples.jar sax.Counter -p org.apache.xerces.parsers.SAXParser data/personal.xml
data/personal.xml: 31 ms (37 elems, 18 attrs, 140 spaces, 128 chars)
(2)java -classpath xercesImpl.jar;xml-apis.jar;xercesSamples.jar sax.Writer data/personal.xml
(3)java -classpath xercesImpl.jar;xml-apis.jar;xercesSamples.jar sax.DocumentTracer data/personal.xml
Step4: run your own java project . like this :
│ .classpath
│ .project
│ Info.xml
│ personal-schema.xml
│ personal.dtd
│ personal.xml
│ personal.xsd
│
├─.settings
│ org.eclipse.jdt.core.prefs
│
├─bin
│ ├─com
│ │ └─kylin
│ │ └─form
│ │ MainWindow.class
│ │
│ └─sax
│ └─helpers
│ AttributesImpl$ListNode.class
│ AttributesImpl.class
│
├─lib
│ xercesImpl.jar
│ xml-apis.jar
│
└─src
├─com
│ └─kylin
│ └─form
│ MainWindow.java
│
└─sax
└─helpers
AttributesImpl.java
MainWindow.java
package com.kylin.form;
import java.io.OutputStreamWriter;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class MainWindow extends DefaultHandler{
/**
* @param args
* @throws SAXException
* @throws Exception
*/
private Locator locator;
private int index1=0;
private int index2=0;
public void characters(char[] ch, int start, int length) throws SAXException {
// TODO 自动生成方法存根
//System.out.print( "SAX Event: CHARACTERS[ " );
System.out.print("本书<<");
try {
OutputStreamWriter outw = new OutputStreamWriter(System.out);
outw.write( ch, start,length );
outw.flush();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(">>的目录");
//System.out.println( " ]" );
}
public void endDocument() throws SAXException {
// TODO 自动生成方法存根
System.out.println( "解析结束:" );
}
public void endElement(String uri, String localName, String qName) throws SAXException {
// TODO 自动生成方法存根
//System.out.println( "SAX Event: END ELEMENT[ " + localName + " ]" );
//this.index2=0;
if(qName.equalsIgnoreCase("chapter"))
this.index2=0;
}
public void setDocumentLocator(Locator locator) {
// TODO 自动生成方法存根
this.locator=locator;
}
public void startDocument() throws SAXException {
// TODO 自动生成方法存根
System.out.println( "解析开始: " );
}
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
// TODO 自动生成方法存根
//System.out.println( "SAX Event: START ELEMENT[ " + localName + " ]" );
if(qName.equalsIgnoreCase("chapter")){
index1++;
for ( int i = 0; i < atts.getLength(); i++ ){
//System.out.println(atts.getLength());
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 {
// TODO 自动生成方法存根
String vendorParserClass= "org.apache.xerces.parsers.SAXParser";
// String xmlURI="http://localhost:8080/TestXml/contents.xml";
String xmlURI="Info.xml";
XMLReader reader =
XMLReaderFactory.createXMLReader(vendorParserClass);
reader.setContentHandler(new MainWindow());
InputSource inputSource=new InputSource(xmlURI);
reader.parse(inputSource);
}
}
result:
解析开始:
本书<<Boss>>的目录
本书<<Big>>的目录
本书<<[email protected]>>的目录
本书<<Worker>>的目录
本书<<One>>的目录
本书<<[email protected]>>的目录
本书<<Worker>>的目录
本书<<Two>>的目录
本书<<[email protected]>>的目录
本书<<Worker>>的目录
本书<<Three>>的目录
本书<<[email protected]>>的目录
本书<<Worker>>的目录
本书<<Four>>的目录
本书<<[email protected]>>的目录
本书<<Worker>>的目录
本书<<Five>>的目录
本书<<[email protected]>>的目录
解析结束: