XML
  Home arrow XML arrow Page 2 - XML Parsing With SAX and Xerces (part ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

XML Parsing With SAX and Xerces (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-02-12

    Table of Contents:
  • XML Parsing With SAX and Xerces (part 2)
  • The Write Stuff
  • Nailing It To The Wall
  • When Things Go Wrong
  • Endnote

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    XML Parsing With SAX and Xerces (part 2) - The Write Stuff


    (Page 2 of 5 )

    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


     

       

    XML ARTICLES

    - 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...
    - Practical XML Data Design and Manipulation f...
    - SimpleXML
    - XForms Basics, Part 3
    - XForms Basics, Part 2
    - XForms Basics





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway