|
Simple API for XML (SAX)
Part:
1
2
What is Simple API for XML (SAX)
SAX: An Application Programming Interface (API) that allows the parser to
fire events based on the XML structure while parsing an XML file. It allows
application programs to process the contents of the XML by handling the events
fired by the parser.
SAX has been implemented in Java in J2SDK 1.4.1_01, which is already installed on my
system. So I am ready to play with XML files through SAX in Java.
SAX Implementations in J2SDK
I have a simple program here, SAXClassChecker.java, to show what are the
implementation classes for the SAX interfaces:
/**
* SAXClassChecker.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
class SAXClassChecker {
public static void main(String[] args) {
try {
File x = new File(args[0]);
SAXParserFactory f = SAXParserFactory.newInstance();
System.out.println(f.toString());
SAXParser p = f.newSAXParser();
System.out.println(p.toString());
DefaultHandler h = new DefaultHandler();
System.out.println(h.toString());
p.parse(x,h);
} catch (ParserConfigurationException e) {
System.out.println(e.toString());
} catch (SAXException e) {
System.out.println(e.toString());
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
Run the program as: java SAXClassChecker hello.xml, you will get:
org.apache.crimson.jaxp.SAXParserFactoryImpl@1c78e57
org.apache.crimson.jaxp.SAXParserImpl@1e63e3d
org.xml.sax.helpers.DefaultHandler@1be2d65
Note that:
- javax.xml.parsers.SAXParserFactory.newInstance() method is used to
create a new fatory instance using the factory implementation class,
org.apache.crimson.jaxp.SAXParserFactoryImpl.
- javax.xml.parsers.SAXParserFactory.newSAXParser() method is used to
create a new parser instance using the parser implementation class,
org.apache.crimson.jaxp.SAXParserImpl.
- javax.xml.parsers.SAXParser.parse() method is used to invoke the
parsing process. It requires two parameters, a File object representing the
XML file, and an parsing event handler object.
- org.xml.sax.helpers.DefaultHandler is dummy handler, which handles all the
events fired by the parser, but does nothing. This is why we are getting nothing
from the XML file when running this program.
SAX Content Handler Interface
As I mentioned earlier, while the SAX parser is parsing an XML file, it fires events.
The first group of such events are designed into an interface, org.xml.sax.ContentHandler.
It contains the following XML content related events:
- startDocument(): When parsing reaches the beginning of the XML document.
- endDocument(): When parsing reaches the end of the XML document.
- startElement(): When parsing reaches the beginning of an XML element.
- endElement(): When parsing reaches the end of an XML element.
- characters(): When parsing reaches the end of an character section.
- ignorableWhitespace(): When parsing reaches any ignorable white spaces between
elements.
Of course, some of the events also pass information parsed from the XML file to the
event handler as parameters. For example:
- startElement() passes all the attributes as an org.xml.sax.Attributes object.
- characters() passes all the characters of the parsed text as char[] object.
(Continued on next part...)
Part:
1
2
|