wood burning stoves*
The moose likes Web Services and the fly likes Using   Xerces 2 Java edition  in your application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Using   Xerces 2 Java edition  in your application" Watch "Using   Xerces 2 Java edition  in your application" New topic
Author

Using Xerces 2 Java edition in your application

corel engine
Greenhorn

Joined: Oct 09, 2012
Posts: 1
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>>的目录
解析结束:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Using Xerces 2 Java edition in your application
 
Similar Threads
How to develop a standalone swing application with ejb 3.1
using abstract method and class
Xml Parsing
why we need to specify DefaultHandler methods in DefaultHandler block in sax
what's the best way to extract multiple attributes from anode