XML
  Home arrow XML arrow Page 2 - XML Parsing With DOM and Xerces (part 2)
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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 DOM and Xerces (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2002-03-06


    Table of Contents:
  • XML Parsing With DOM and Xerces (part 2)
  • The Writing On The Wall
  • Highlights
  • Data Overload
  • Oops!
  • Dear Diary
  • Of Method And Madness
  • Black Or White
  • Link Out

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    XML Parsing With DOM and Xerces (part 2) - The Writing On The Wall
    ( Page 2 of 9 )

    Let's begin by quickly revisiting the sample XML file used earlier:

    <?xml version="1.0"?> <inventory> <!-- time to lock and load --> <item> <id>758</id> <name>Rusty, jagged nails for nailgun</name> <supplier>NailBarn, Inc.</supplier> <cost currency="USD">2.99</cost> A <quantity alert="500">10000</quantity> </item> <item> <id>6273</id> <name>Power pack for death ray</name> <supplier>QuakePower.domain.com</supplier> <cost currency="USD">9.99</cost> <quantity alert="20">10</quantity> </item> </inventory>
    This next example ports the dynamically-printed tree structure you saw in a previous example to work with a Web server.

    import org.apache.xerces.parsers.DOMParser; import org.xml.sax.SAXException; import org.w3c.dom.*; import java.io.*; public class MyFourthDomApp { private Writer out; String Content = ""; // a counter for keeping track of the "tabs" private int TabCounter = 0; // constructor public MyFourthDomApp (String xmlFile, Writer out) { this.out = out; // create a Xerces DOM parser DOMParser parser = new DOMParser(); // parse the document and // access the root node with its children. try { parser.parse(xmlFile); Document document = parser.getDocument(); NodeDetails(document); } catch (SAXException e) { // something went wrong! } catch (IOException e) { // something went wrong! } } // recursively traverse the document tree private void NodeDetails (Node node) { try { int type = node.getNodeType(); if (type == Node.ELEMENT_NODE) { // if element FormatTree(TabCounter); out.write ("Element: " + node.getNodeName() + "<br>"); if(node.hasAttributes()) { NamedNodeMap AttributesList = node.getAttributes(); for(int j = 0; j < AttributesList.getLength(); j++) { FormatTree(TabCounter); out.write("Attribute: " + AttributesList.item(j).getNodeName() + " = " + AttributesList.item(j).getNodeValue() + "<br>"); } } } else if (type == Node.TEXT_NODE) { // if character data Content = node.getNodeValue(); if (!Content.trim().equals("")){ FormatTree(TabCounter); out.write ("Character Data: " + Content + "<br>"); } } else if (type == Node.COMMENT_NODE) { // if comment Content = node.getNodeValue(); if (!Content.trim().equals("")){ FormatTree(TabCounter); out.write("Comment: " + Content + "<br>"); } // add code for other node types here if you like } NodeList children = node.getChildNodes(); if (children != null) { for (int i=0; i< children.getLength(); i++) { TabCounter++; NodeDetails(children.item(i)); TabCounter--; } } } catch (IOException e) { // something went wrong! } } // this formats the output as a tree private void FormatTree (int TabCounter) { try { for(int j = 1; j < TabCounter; j++) { out.write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"); } } catch (IOException e) { // something went wrong! } } }
    The most important difference between this example and the previous one is the introduction of a new Writer object, which makes it possible to redirect 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 MyFourthDomApp (String xmlFile, Writer out) { // snip! }
    Note that, since I'm using a Writer object, I also need to add some code to trap and resolve IOExceptions, if they occur. I've left that aside for a more detailed discussion a little later - however, you should be aware of this requirement and design your code appropriately.

    You'll notice that I haven't made any major changes to the NodeDetails() function, other than the inclusion of HTML tags in the output - this is what will ultimately get sent to the browser. The only item left is to handle the connection between the Java class above and the Web server - which is where JSP comes in.

    Here's the JSP page that brings it all together:

    <%@ page language="java" import="java.io.IOException" %> <html> <head> </head> <body> <% try { MyFourthDomApp myFourthExample = new MyFourthDomApp("/www/xerces/WEB-INF/classes/inventory.xml",out); } catch (Exception e) { out.println("Something bad happened!" + e); } %> </body> </html>
    And here's what the output looks like:



    For the moment, I'll ignore any errors that may occur when processing the JSP page, deferring this to a later discussion.

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

       

    XML ARTICLES

    - 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...
    - Practical XML Data Design and Manipulation f...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT