2.2 Java SAX Programs
- Get a parser factory
- Communicate to the factory the desired properties of the parser
- Get the parser
- Query the parser for the reader assigned to process the input
- Register the event handlers with the reader
- Provide the input to the reader and start parsing it
import javax.xml.parsers.*;
import org.xml.sax.XMLReader;
import java.io.File;
class SAXOutline {
static public void main(String[] args)
throws Exception {
// get the factory
SAXParserFactory factory =
SAXParserFactory.newInstance();
// default properties
// get a parser
SAXParser saxParser =
factory.newSAXParser();
// get a reader
XMLReader xmlReader =
saxParser.getXMLReader();
// default events handler
// process the input
xmlReader.parse
( new File(args[0]).toURL().toString() );
} }
-_-_-
Note. The above program can be used to test whether given files are in XML format
(javac SAXOutline.java ; java SAXOutline filename).
SAXParserFactory, SAXParser, XMLReader