java/ch/wlkl/javaExamples/ExXml.java
/*
* ExXml.java
*
* Created on 29. Februar 2004, 21:28
*/
/**
*
* @author walter
*/
package ch.wlkl.javaExamples;
// import org.apache.crimson.jaxp.*;
// import org.apache.xerces.jaxp.*;
// import org.apache.xerces.util.*;
import javax.xml.parsers.*;
import org.xml.sax.helpers.*;
import org.xml.sax.*;
import org.w3c.dom.*;
public class ExXml {
/** Creates a new instance of ExXml */
public ExXml() {
}
DefaultHandler myHandler() {
return new DefaultHandler() {
int lev = 0;
public void startElement(String uri, String localName,
String qName, Attributes attributes) throws SAXException {
System.out.println("startElement " + ++lev + " " + uri + ":"
+ localName + ", q " + qName);
for (int ix = 0; ix < attributes.getLength(); ix++)
System.out.println("attribute " + attributes.getURI(ix)
+ ":" + attributes.getLocalName(ix) + " '"
+ attributes.getValue(ix) + "'");
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("endElement " + lev-- + " " + uri + ":"
+ localName + ", q " + qName);
}
public void characters(char ch[], int start, int length)
throws SAXException {
System.out.println("characters " + length + " '"
+ new String(ch, start, length) + "'");
// no op
}
public void ignorableWhiteSpace(char ch[], int start, int length)
throws SAXException {
System.out.println("ignoreSpace " + length + "'"
+ new String(ch, start, length) + "'");
// no op
};
};
};
/**
* @param args
* the command line arguments
*/
void saxParser(String file) throws Exception {
SAXParserFactory pf = SAXParserFactory.newInstance();
System.out.println("found1 parser " + pf + ", isNamespaceAwarena "
+ pf.isNamespaceAware() + " isValidating() "
+ pf.isValidating());
pf.setNamespaceAware(true);
pf.setValidating(true);
System.out.println("found2 parser " + pf + ", isNamespaceAwarena "
+ pf.isNamespaceAware() + " isValidating() "
+ pf.isValidating());
SAXParser p = pf.newSAXParser();
System.out.println(" parser class " + p.getClass());
p.parse(Java.packagePrefix() + file, myHandler());
}
void saxParser2(String file) throws Exception {
XMLReader p = XMLReaderFactory
.createXMLReader("org.apache.xerces.parsers.SAXParser");
System.out.println(" parser class " + p.getClass() + " p " + p);
System.out
.println("schema-full-checking "
+ p
.getFeature("http://apache.org/xml/features/validation/schema-full-checking"));
p.setFeature("http://xml.org/sax/features/validation", true);
p.setFeature("http://apache.org/xml/features/validation/schema", true);
p
.setFeature(
"http://apache.org/xml/features/validation/schema-full-checking",
true);
System.out
.println("schema-full-checking "
+ p
.getFeature("http://apache.org/xml/features/validation/schema-full-checking"));
System.out.println("namespaces "
+ p.getFeature("http://xml.org/sax/features/namespaces"));
System.out.println("validation "
+ p.getFeature("http://xml.org/sax/features/validation"));
System.out
.println("schema "
+ p
.getFeature("http://apache.org/xml/features/validation/schema"));
p.setContentHandler(myHandler());
// p.setErrorHandler(new DefaultErrorHandler());
p.parse(Java.packagePrefix() + file);
}
void saxReader(String file) throws Exception {
XMLReader myReader = XMLReaderFactory.createXMLReader();
}
void dom(String file) throws Exception {
DocumentBuilderFactory pf = DocumentBuilderFactory.newInstance();
DocumentBuilder p = pf.newDocumentBuilder();
System.out.println("found1 parser " + pf + ", isNamespaceAwarena "
+ pf.isNamespaceAware() + " isValidating() "
+ pf.isValidating() + ", cla " + p.getClass());
pf.setNamespaceAware(true);
pf.setValidating(true);
System.out.println("found2 parser " + pf + ", isNamespaceAwarena "
+ pf.isNamespaceAware() + " isValidating() "
+ pf.isValidating() + ", cla " + p.getClass());
Document doc = p.parse(file);
traverse(doc, 0);
}
void traverse(Node n, int lev) {
String t = "";
for (int i = lev; i > 0; i--)
t += ' ';
t += n.getNodeName() + ": " + n.getNodeType() + " \""
+ n.getNodeValue() + '"';
System.out.println(t);
for (int ix = 0; ix < n.getChildNodes().getLength(); ix++)
traverse(n.getChildNodes().item(ix), lev + 1);
}
public static void main(String[] args) {
(new Java(args, new String[] { "saxParser2", "w1.xml" }))
.invokeMethods(new ExXml());
}
}