import java.io.IOException; import org.xml.sax.XMLReader; import org.xml.sax.SAXException; import org.xml.sax.ContentHandler; import org.xml.sax.Locator; import org.xml.sax.Attributes; import org.apache.xerces.parsers.SAXParser; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXParseException; class MyErrorHandler implements ErrorHandler { public void error(SAXParseException exception) { System.out.println("SAX nonfatal error: " + exception.getMessage()); } public void fatalError(SAXParseException exception) { System.out.println("SAX fatal error: " + exception.getMessage()); } public void warning(SAXParseException exception) { System.out.println("SAX warning: " + exception.getMessage()); } } class MyContentHandler implements ContentHandler { private int level = 0; public static void indent(int llevel) { for(int i=0; i < llevel; i++) {System.out.print(" ");} } // Receive notification of character data. public void characters(char[] ch, int start, int length) { String s = new String(ch, start, length); if (s.trim() != "") { indent(level + 1); System.out.print("TEXT : noname : "); System.out.println(s); } } // Receive notification of the end of a document. public void endDocument() { indent(level); System.out.println("endDocument()"); } // Receive notification of the end of an element. public void endElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName) { level--; indent(level); System.out.println("endElement(namespaceURI=" + namespaceURI + ",localName=" + localName + ",qName=" + qName +")"); } // End the scope of a prefix-URI mapping. public void endPrefixMapping(java.lang.String prefix) { indent(level); System.out.println("endPrefixMapping(prefix=" + prefix +")"); } // Receive notification of ignorable whitespace in element content. public void ignorableWhitespace(char[] ch, int start, int length) { indent(level); String s = new String(ch, start, length); System.out.println("ignorableWhitespace(string=" + s + ")"); } // Receive notification of a processing instruction. public void processingInstruction(java.lang.String target, java.lang.String data) { indent(level); System.out.println("processingInstruction(target=" + target + ",data=" + data + ")"); } // Receive an object for locating the origin of SAX document events. public void setDocumentLocator(Locator locator) { indent(level); System.out.println("setDocumentLocator()"); } // Receive notification of a skipped entity. public void skippedEntity(java.lang.String name) { indent(level); System.out.println("skippedEntity(name=" + name + ")"); } // Receive notification of the beginning of a document. public void startDocument() { indent(level); System.out.println("startDocument()"); } // Receive notification of the beginning of an element. public void startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, Attributes atts) { indent(level); System.out.print("ELEMENT : "); System.out.print(localName); System.out.print(" : novalue : "); System.out.print("("); for(int i=0; i < atts.getLength(); i++) { if(i > 0) {System.out.print(",");} System.out.print(atts.getLocalName(i) + "=\"" + atts.getValue(i) + "\""); } System.out.print(")"); System.out.println(); level++; } // Begin the scope of a prefix-URI Namespace mapping. public void startPrefixMapping(java.lang.String prefix, java.lang.String uri) { indent(level); System.out.println("startPrefixMapping(prefix=" + prefix + ",uri=" + uri + ")"); } } class Hello { public static void main(String[] args) { System.out.println("parsing " + args[0] + "... "); try { XMLReader parser = new SAXParser(); ContentHandler handler = new MyContentHandler(); parser.setContentHandler(handler); ErrorHandler errHandler = new MyErrorHandler(); parser.setErrorHandler(errHandler); parser.parse(args[0]); } catch (IOException e) { System.out.print("IO Exception: " + e.getMessage()); } catch (SAXException e) { System.out.print("SAX Exception: " + e.getMessage()); } System.out.println("Done!"); } }