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>>的目录
本书<<chief@foo.com>>的目录
本书<<Worker>>的目录
本书<<One>>的目录
本书<<one@foo.com>>的目录
本书<<Worker>>的目录
本书<<Two>>的目录
本书<<two@foo.com>>的目录
本书<<Worker>>的目录
本书<<Three>>的目录
本书<<three@foo.com>>的目录
本书<<Worker>>的目录
本书<<Four>>的目录
本书<<four@foo.com>>的目录
本书<<Worker>>的目录
本书<<Five>>的目录
本书<<five@foo.com>>的目录
解析结束:
分享到:
相关推荐
Xerces-Java
这是java解析xml的xerces-2_11_0解析器,是java语言解析xml的必备解析工具。正常使用的,好用!
This will keep your application shielded from changes in the underlying implementation of those standard APIs and also gives you more flexibility to change the implementation of the standard pieces ...
Xerces-C++ makes it easy to give your application the ability to read and write XML data. A shared library is provided for parsing, generating, manipulating, and validating XML documents using the ...
Xerces-C++ makes it easy to give your application the ability to read and write XML data. A shared library is provided for parsing, generating, manipulating, and validating XML documents using the ...
Xerces解析文档Xerces解析文档Xerces解析文档Xerces解析文档
XML4C 和 XML4J 是两个并列的项目,而 XML4J 是 Xerces-J——Java 实现——的前身。IBM 将这两个项目的源代码让与 Apache 软件基金会(Apache Software Foundation),他们将其分别改名为 Xerces-C++ 和 Xerces-J。注...
C++开源的xml解析器,当前最新的版本xerces-c-src_2_8_0。Xerces-C++ 的前身是 IBM 的 XML4C 项目。XML4C 和 XML4J 是两个并列的项目,IBM 将这两个项目的源代码让与 Apache 软件基金会,他们将其分别改名为 Xerces-...
xerces-c-3.2.3的64位库,由VS 2015编译而成。Xerces是由Apache组织所推动的一项XML文档解析开源项目,它目前有多种语言版本包括JAVA、C++、PERL、COM等。
XML4C 和 XML4J 是两个并列的项目,而 XML4J 是 Xerces-J——Java 实现——的前身。IBM 将这两个项目的源代码让与 Apache 软件基金会(Apache Software Foundation),他们将其分别改名为 Xerces-C++ 和 Xerces-J。注...
Xerces-J-tools.2.9.0,官网:Xerces-J-tools.2.9.0
xerces-c2_2_0-win32.zip安装软件。Xerces是一个开放源代码的XML语法分析器。
Xerces Java包: org.apache.xerces.dom——Xerces DOM实现 org.apache.xerces.parsers——Xerces解析器;DOM和SAX org.apache.xerces.serialize——将DOM树序列化到文件、输出流和编写器
XML4C 和 XML4J 是两个并列的项目,而 XML4J 是 Xerces-J——Java 实现——的前身。IBM 将这两个项目的源代码让与 Apache 软件基金会(Apache Software Foundation),他们将其分别改名为 Xerces-C++ 和 Xerces-J。 这...
Xerces-c++指南
Xerces分析器可处理Java和C++,它采用互联网联盟XML、文件对象模型以及用于XML的简单API标准。所有的Xerces分析器都是模块化可配置的。它为C++版本提供了Perl封装,允许访问Perl的XML分析器,它还提供对统一编码字符...
Xerces-J最新源码
xerces.jar xerces.jar xerces.jar xerces.jar xerces.jar