Java Reference
In-Depth Information
characters.");
}
}
Directory "TrySAXHandler 1"
Each handler method just outputs information about the event to the command line.
Now you can define a program to use a handler of this class type to parse an XML document. You can
make the example read the name of the XML file to be processed from the command line. Here's the
code:
import javax.xml.parsers.*;
import org.xml.sax.SAXException;
import java.io.*;
public class TrySAXHandler {
public static void main(String args[]) {
if(args.length == 0) {
System.out.println("No file to process. Usage is:" +
"\njava TrySax
\"filename\" ");
return;
}
File xmlFile = new File(args[0]);
if(xmlFile.exists()) {
process(xmlFile);
} else {
System.out.println(xmlFile + " does not exist.");
}
}
private static void process(File file) {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser = null;
spf.setNamespaceAware(true);
spf.setValidating(true);
System.out.println("Parser will " +
(spf.isNamespaceAware() ? "" : "not ") + "be namespace
aware");
System.out.println("Parser will " + (spf.isValidating() ? "" :
"not ")
+
"validate XML");
try {
parser = spf.newSAXParser();
System.out.println("Parser object is: " + parser);
Search WWH ::




Custom Search