Home arrow XML arrow 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.

TABLE OF CONTENTS:
  1. XML Parsing With SAX and Xerces (part 2)
  2. The Write Stuff
  3. Nailing It To The Wall
  4. When Things Go Wrong
  5. Endnote
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 1
February 12, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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 ("&nbsp;&nbsp;&nbsp;Type: "" + AttributeType + ""<br>"); out.write ("&nbsp;&nbsp;&nbsp;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.

Here's the code:

<%@ page language="java" import="java.io.IOException" %> <html> <head> </head> <body> <% try { MyFourthSaxApp myFourthExample = new MyFourthSaxApp("/www/xerces/WEB-INF/classes/inventory.xml",out); } catch (Exception e) { out.println("Something bad happened!"); } %> </body> </html>
The output of this example is the HTML equivalent of the output of the previous example. Here's what it looks like:



 
 
>>> More XML Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

XML ARTICLES

- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- How to Set Up Podcasting and Vodcasting
- Creating an RSS Reader Application
- Building an RSS File
- An Introduction to XUL Part 6
- An Introduction to XUL Part 5
- An Introduction to XUL Part 4
- An Introduction to XUL Part 3
- An Introduction to XUL Part 2
- An Introduction to XUL Part 1
- XML Matters: Practical XML Data Design and M...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: