1 import org.xml.sax.ContentHandler ; 2 import org.xml.sax.Attributes ; 3 import org.xml.sax.SAXException ; 4 import org.xml.sax.Locator ; 5 6 7 public class ExampleContentHandler implements ContentHandler { 8 9 public void setDocumentLocator(Locator locator) { 10 System.out.println("setDocumentLocator"); 11 } 12 13 public void startDocument() throws SAXException { 14 System.out.println("startDocument"); 15 } 16 17 public void endDocument() throws SAXException { 18 System.out.println("endDocument"); 19 } 20 21 public void startPrefixMapping(String prefix, String uri) 22 throws SAXException { 23 System.out.println("startPrefixMapping: " + prefix + ", " + uri); 24 } 25 26 public void endPrefixMapping(String prefix) throws SAXException { 27 System.out.println("endPrefixMapping: " + prefix); 28 } 29 30 public void startElement( 31 String namespaceURI, String localName, String qName, Attributes atts) 32 throws SAXException { 33 34 System.out.print("startElement: " + namespaceURI + ", " 35 + localName + ", " + qName); 36 37 int n = atts.getLength(); 38 39 for (int i = 0; i < n; i++) { 40 System.out.print(", " + atts.getQName(i) + "='" + atts.getValue(i) + "'"); 41 } 42 43 System.out.println(""); 44 } 45 46 public void endElement( 47 String namespaceURI, String localName, String qName) 48 throws SAXException { 49 System.out.println("endElement: " + namespaceURI + ", " 50 + localName + ", " + qName); 51 } 52 53 public void characters(char ch[], int start, int length) 54 throws SAXException { 55 56 String s = new String (ch, start, (length > 30) 57 ? 30 58 : length); 59 60 if (length > 30) { 61 System.out.println("characters: \"" + s + "\"..."); 62 } else { 63 System.out.println("characters: \"" + s + "\""); 64 } 65 } 66 67 public void ignorableWhitespace(char ch[], int start, int length) 68 throws SAXException { 69 System.out.println("ignorableWhitespace"); 70 } 71 72 public void processingInstruction(String target, String data) 73 throws SAXException { 74 System.out.println("processingInstruction: " + target + ", " 75 + data); 76 } 77 78 public void skippedEntity(String name) throws SAXException { 79 System.out.println("skippedEntity: " + name); 80 } 81 82 public static void main(String [] args) throws Exception { 83 org.xml.sax.XMLReader parser = 84 javax.xml.parsers.SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 85 System.err.println("Parser: " + parser.getClass()); 86 parser.setContentHandler(new ExampleContentHandler()); 87 parser.parse(new java.io.File (args[0]).toURL().toString()); 88 } 89 } 90 | Popular Tags |