HomeXML Page 2 - XML Parsing With SAX and Xerces (part 2)
The Write Stuff - XML
The first part of this article demonstrated the basics of the Xerces XML parser, explaining how it could be used to process XML documents in a non-Web environment. This concluding section closes the circle, taking everything you've learned so far and demonstrating how it can be applied to create dynamic Web pages from static XML documents with Xerces.
As in the first part of this article, we'll begin with something simple.
Let's go back to that XML file I created in the first part of this article:
<?xml version="1.0"?>
<inventory>
<item>
<id>758</id>
<name>Rusty,
jagged nails for nailgun</name>
<supplier>NailBarn, Inc.</supplier>
<cost>2.99</cost>
<quantity>10000</quantity>
</item>
<item>
<id>6273</id>
<name>Power
pack for death ray</name>
<supplier>QuakePower.domain.com</supplier>
<cost>9.99</cost>
<quantity>10</quantity>
</item>
</inventory>
Remember that event trail you saw in one of the very first examples? This next
example ports that code to work with a Web server.
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import
java.io.*;
public class MyFourthSaxApp extends DefaultHandler {
private Writer
out;
// constructor
public MyFourthSaxApp (String xmlFile, Writer out)
{
this.out = out;
// create a Xerces SAX parser
SAXParser parser = new SAXParser();
// set the content handler
parser.setContentHandler(this);
// parse the document
try {
parser.parse(xmlFile);
out.flush();
}
catch (SAXException e) {
// something went wrong!
} catch
(IOException e) {
// something went wrong!
}
}
//
call at document start
public void startDocument() {
try {
out.write
("<h1>Document
begins</h1><br>");
} catch (IOException e) {
//
do nothing
}
}
//
call at element start
public void startElement (String
uri, String local,
String qName,
Attributes atts) {
try {
out.write
("<h2>Element
begins: "" + local + ""</h2>");
String AttributeName,AttributeType,AttributeValue
= "";
for (int i = 0; i < atts.getLength(); i++) {
AttributeName =
atts.getLocalName(i);
AttributeType
= atts.getType(AttributeName);
AttributeValue
= atts.getValue(AttributeName);
out.write
("<h3>Attribute: "" + AttributeName
+ ""<br>");
out.write (" Type:
"" + AttributeType
+ ""<br>");
out.write (" Value:
"" + AttributeValue
+
""<br></h3>");
}
} catch (IOException
e) {
// do nothing
}
}
// call when cdata found
public void characters(char[]
text,
int start, int length) {
try {
String Content = new String(text, start,
length);
if (!Content.trim().equals("")){
out.write("<h4>Character
data: "" + Content + ""<br></h4>");
}
} catch (IOException e) {
//
do nothing
}
}
// call at element end
public void endElement (String
uri, String local, String qName){
try {
out.write("<h2> Element ends:
"" + local + ""<br></h2>");
} catch (IOException e) {
// do nothing
}
}
//
call at document end
public void endDocument() {
try {
out.write
("<h1>Document
ends</h1><br>");
} catch (IOException e) {
// do
nothing
}
}
}
I don't want to get into the details of the callbacks here - refer to the explanation
for the original example if there's something that doesn't seem to make sense - but I will point out some items of interest.
The most important difference between this example and the previous one is the introduction of a new Writer object, which makes it possible to stream output to the browser instead of the standard output device.
private Writer out;
The constructor also needs to be modified to accept two parameters: the name
of the XML file, and a reference to the Writer object.
// constructor
public MyFourthSaxApp (String xmlFile, Writer out) {
this.out
= out;
// constructor code comes here<sum>
}
This Writer object will be used to output HTML code to the browser, thereby enabling
the dynamic generation of a Web page - as the following snippets demonstrates:
out.write("<h2>Element begins: "" + local + ""</h2>");
out.write ("<h1>Document
ends</h1><br>");
Once this class has been compiled, it can easily be imported and used in a JSP
document, thereby immediately making the application Web-friendly.