KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ExampleContentHandler


1 import org.xml.sax.ContentHandler JavaDoc;
2 import org.xml.sax.Attributes JavaDoc;
3 import org.xml.sax.SAXException JavaDoc;
4 import org.xml.sax.Locator JavaDoc;
5
6
7 public class ExampleContentHandler implements ContentHandler JavaDoc {
8
9     public void setDocumentLocator(Locator JavaDoc locator) {
10         System.out.println("setDocumentLocator");
11     }
12
13     public void startDocument() throws SAXException JavaDoc {
14         System.out.println("startDocument");
15     }
16
17     public void endDocument() throws SAXException JavaDoc {
18         System.out.println("endDocument");
19     }
20
21     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
22             throws SAXException JavaDoc {
23         System.out.println("startPrefixMapping: " + prefix + ", " + uri);
24     }
25
26     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
27         System.out.println("endPrefixMapping: " + prefix);
28     }
29
30     public void startElement(
31             String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
32                 throws SAXException JavaDoc {
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 JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
48                 throws SAXException JavaDoc {
49         System.out.println("endElement: " + namespaceURI + ", "
50                            + localName + ", " + qName);
51     }
52
53     public void characters(char ch[], int start, int length)
54             throws SAXException JavaDoc {
55
56         String JavaDoc s = new String JavaDoc(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 JavaDoc {
69         System.out.println("ignorableWhitespace");
70     }
71
72     public void processingInstruction(String JavaDoc target, String JavaDoc data)
73             throws SAXException JavaDoc {
74         System.out.println("processingInstruction: " + target + ", "
75                            + data);
76     }
77
78     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
79         System.out.println("skippedEntity: " + name);
80     }
81     
82     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
83         org.xml.sax.XMLReader JavaDoc 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 JavaDoc(args[0]).toURL().toString());
88     }
89 }
90
Popular Tags