java/ch/wlkl/javaExamples/ExXml.java~
/*
* ExXml.java
*
* Created on 29. Februar 2004, 21:28
*/
/**
*
* @author walter
*/
package WkJava;
import org.apache.crimson.jaxp.*;
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() {
}
/**
* @param args the command line arguments
*/
void sax(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(Ex.packagePrefix() + file, 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 + ", " + attributes.getLength() + " atts");
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("endElement " + lev-- + " (" + uri + ", local " + 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
}
});
}
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 Ex( args, new String [] { "sax", "rlTestData.xml"} )).invokeMethods(new ExXml());
}
}