SAX2 Event Reporter
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
public class EventReporter implements ContentHandler {
public void setDocumentLocator(Locator locator) {
System.out.println("setDocumentLocator(" + locator + ")");
}
public void startDocument() throws SAXException {
System.out.println("startDocument()");
}
public void endDocument() throws SAXException {
System.out.println("endDocument()");
}
public void startElement(String namespaceURI, String localName,
String qualifiedName, Attributes atts)
throws SAXException {
namespaceURI = '"' + namespaceURI + '"';
localName = '"' + localName + '"';
qualifiedName = '"' + qualifiedName + '"';
String attributeString = "{";
for (int i = 0; i < atts.getLength(); i++) {
attributeString += atts.getQName(i) + "=\""
+ atts.getValue(i) + "\"";
if (i != atts.getLength()-1) attributeString += ", ";
}
attributeString += "}";
System.out.println("startElement(" + namespaceURI + ", "
+ localName + ", " + qualifiedName + ", " + attributeString + ")");
}
public void endElement(String namespaceURI, String localName,
String qualifiedName)
throws SAXException {
namespaceURI = '"' + namespaceURI + '"';
localName = '"' + localName + '"';
qualifiedName = '"' + qualifiedName + '"';
System.out.println("endElement(" + namespaceURI + ", "
+ localName + ", " + qualifiedName + ")");
}
public void characters(char[] text, int start, int length)
throws SAXException {
String textString = "[" + new String(text) + "]";
System.out.println("characters(" + textString + ", "
+ start + ", " + length + ")");
}
public void ignorableWhitespace(char[] text, int start, int length)
throws SAXException {
System.out.println("ignorableWhitespace()");
}
public void processingInstruction(String target, String data)
throws SAXException {
System.out.println("processingInstruction(" + target + ", "
+ data + ")");
}
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
System.out.println("startPrefixMapping(\"" + prefix + "\", \""
+ uri + "\")");
}
public void endPrefixMapping(String prefix) throws SAXException {
System.out.println("endPrefixMapping(\"" + prefix + "\")");
}
public void skippedEntity(String name) throws SAXException {
System.out.println("skippedEntity(" + name + ")");
}
// Could easily have put main() method in a separate class
public static void main(String[] args) {
XMLReader parser;
try {
parser = XMLReaderFactory.createXMLReader();
}
catch (Exception e) {
// fall back on Xerces parser by name
try {
parser = XMLReaderFactory.createXMLReader(
"org.apache.xerces.parsers.SAXParser");
}
catch (Exception ee) {
System.err.println("Couldn't locate a SAX parser");
return;
}
}
if (args.length == 0) {
System.out.println(
"Usage: java EventReporter URL1 URL2...");
}
// Install the content handler
parser.setContentHandler(new EventReporter());
// start parsing...
for (int i = 0; i < args.length; i++) {
// command line should offer URIs or file names
try {
parser.parse(args[i]);
}
catch (SAXParseException e) { // well-formedness error
System.out.println(args[i] + " is not well formed.");
System.out.println(e.getMessage()
+ " at line " + e.getLineNumber()
+ ", column " + e.getColumnNumber());
}
catch (SAXException e) { // some other kind of error
System.out.println(e.getMessage());
}
catch (IOException e) {
System.out.println("Could not report on " + args[i]
+ " because of the IOException " + e);
}
}
}
}