SAX Parser – Simple API for XML
Apache parser
Simple API for XML (SAX)
The Apache parser version 1.2.3 (commonly known as Xerces) is an open-source effort based on IBM’s XML4J parser. Xerces has full support for the W3C Document Object Model (DOM) Level 1 and the Simple API for XML (SAX) 1.0 and 2.0; however it currently has only limited support for XML Schemas, DOM Level 2 (version 1). You can use Xalan, also available from Apache’s Web site, for XSLT processing. You can configure both the DOM and SAX parsers.
Xerces uses the SAX2 method getFeature() and setFeature() to query and set various parser features.
Note: Add the xerces.jar file to your CLASSPATH to use the parser.
recipes.xml
<?xml version="1.0" encoding="UTF-8"?>
<collection xmlns="http://recipes.org">
<recipe>
<ingredient name="flour" amount="4" unit="cup"/>
</recipe>
<recipe>
<ingredient name="flour" amount="3" unit="cup"/>
</recipe>
<recipe>
<ingredient name="flour" amount="0.25" unit="cup"/>
</recipe>
</collection>
The following Java programs reads the recipe collection and outputs the total amount of flour being used (assuming the unit is always cup)
Flour.java
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.apache.xerces.parsers.SAXParser;
public class Flour extends DefaultHandler {
float amount = 0;
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) {
if (namespaceURI.equals("http://recipes.org") && localName.equals("ingredient")) {
String n = atts.getValue("","name");
if (n.equals("flour")) {
String a = atts.getValue("","amount"); // assume 'amount' exists
amount = amount + Float.valueOf(a).floatValue();
}
}
}
public static void main(String[] args) {
Flour f = new Flour();
SAXParser p = new SAXParser();
p.setContentHandler(f);
try
{
p.parse(args[0]);
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println(f.amount);
}
}
NOTE: You need xerces.jar file in classpath to run above program.
Some other Parser
Oracle Parser
The Oracle parser implements DOM Level 1, and SAX 1.0 and 2.0. It has a partial implementation of DOM Level 2 and includes APIs for XSLT.
It supports Schemas through the oracle.xml.parser.schema package.
The DOM class shown earlier requires relatively few changes to compile and run with the Oracle parser.
Change the second import line so it refers to the Oracle parser.
The parse() method, expects a string containing a URL, not a physical path and filename. Alternately, you can pass the parse() method a URL object.
import org.w3c.dom.*;
import oracle.xml.parser.v2.DOMParser;
public class DOM
{
public static void main(String[] args)
{
try {
DOMParser parser = new DOMParser();
String url = "file://C|/xml/code/" + args[0];
parser.parse(url);
Document doc = parser.getDocument();
NodeList nodes = doc.getElementsByTagName("servlet");
System.out.println("There are " + nodes.getLength() +
" elements.");
} catch (Exception ex) {
System.out.println(ex);
}
}
}
Sun Parser
Sun packages its XML APIs as the Java API for XML Processing (JAXP).
I used an early relase latest version (1.1). Like Oracle, Sun incorporates support for DOM, SAX, Schema and XSL into its parser.
The XML Parser is based on the Project X parser from Sun and the XSLT processor is actually Xalan from Apache.
Using factory classes, JAXP allows you to plug in any conforming XML or XSL parser, thus creating a standard mechanism for Java applications to interact with XML parsers.
The parser supports SAX 2.0, DOM Level 2 and XSLT 1.0.
You need to add three jar files
1. jaxp.jar,
2. xercesImpl.jar, and
3. xalan.jar
to your CLASSPATH.
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.*;
1. Factory classes makes the process of initiating the parser a bit different
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(args[0]);
2. The factory to get the parser directly
SAX SAXHandler = new SAX();
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sparser = spf.newSAXParser();
sparser.parse(args[0], SAXHandler);
3. Interact indirectly with a SAX parser
SAX SAXHandler = new SAX();
SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = null;
try {
SAXParser saxParser = spf.newSAXParser();
xmlReader = saxParser.getXMLReader();
} catch (Exception ex) {
System.out.println(ex);
}
xmlReader.setContentHandler(SAXHandler);
xmlReader.setErrorHandler(SAXHandler);
try {
xmlReader.parse(args[0]);
} catch (SAXException ex) {
System.out.println(ex);
}
Recent Comments