Java Reference
In-Depth Information
Example 19−1: ListServlets1.java (continued)
// Configure the parser factory for the type of parsers we require
spf.setValidating(false); // No validation required
// Now use the parser factory to create a SAXParser object
// Note that SAXParser is a JAXP class, not a SAX class
javax.xml.parsers.SAXParser sp = spf.newSAXParser();
// Create a SAX input source for the file argument
org.xml.sax.InputSource input=new InputSource(new FileReader(args[0]));
// Give the InputSource an absolute URL for the file, so that
// it can resolve relative URLs in a <!DOCTYPE> declaration, e.g.
input.setSystemId("file://" + new File(args[0]).getAbsolutePath());
// Create an instance of this class; it defines all the handler methods
ListServlets1 handler = new ListServlets1();
// Finally, tell the parser to parse the input and notify the handler
sp.parse(input, handler);
// Instead of using the SAXParser.parse() method, which is part of the
// JAXP API, we could also use the SAX1 API directly. Note the
// difference between the JAXP class javax.xml.parsers.SAXParser and
// the SAX1 class org.xml.sax.Parser
//
// org.xml.sax.Parser parser = sp.getParser(); // Get the SAX parser
// parser.setDocumentHandler(handler);
// Set main handler
// parser.setErrorHandler(handler);
// Set error handler
// parser.parse(input);
// Parse!
}
StringBuffer accumulator = new StringBuffer(); // Accumulate parsed text
String servletName;
// The name of the servlet
String servletClass;
// The class name of the servlet
String servletId;
// Value of id attribute of <servlet> tag
// When the parser encounters plain text (not XML elements), it calls
// this method, which accumulates them in a string buffer
public void characters(char[] buffer, int start, int length) {
accumulator.append(buffer, start, length);
}
// Every time the parser encounters the beginning of a new element, it
// calls this method, which resets the string buffer
public void startElement(String name, AttributeList attributes) {
accumulator.setLength(0); // Ready to accumulate new text
// If its a servlet tag, look for id attribute
if (name.equals("servlet"))
servletId = attributes.getValue("id");
}
// When the parser encounters the end of an element, it calls this method
public void endElement(String name) {
if (name.equals("servlet-name")) {
// After </servlet-name>, we know the servlet name saved up
servletName = accumulator.toString().trim();
}
else if (name.equals("servlet-class")) {
Search WWH ::




Custom Search