XML
  Home arrow XML arrow Page 6 - XML Parsing With DOM 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 DOM and Xerces (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 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:
      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 DOM and Xerces (part 2) - Dear Diary


    (Page 6 of 9 )

    Let's try one more example, this one demonstrating an alternative technique of formatting an XML document into HTML Here's the XML file:

    <?xml version="1.0"?> <todo> <item> <priority>1</priority> <task>Figure out how Xerces works</task> <due>2001-12-12</due> </item> <item> <priority>2</priority> <task>Conquer the last Quake map</task> <due>2001-12-31</due> </item> <item> <priority>3</priority> <task>Buy a Ferrari</task> <due>2005-12-31</due> </item> <item> <priority>1</priority> <task>File tax return</task> <due>2002-03-31</due> </item> <item> <priority>3</priority> <task>Learn to cook</task> <due>2002-06-30</due> </item> </todo>
    Here's what it should look like:



    As with the previous examples, this has two components: the source code for the Java class, and the JSP page which uses the class. Here's the class:

    import org.apache.xerces.parsers.DOMParser; import org.xml.sax.SAXException; import org.w3c.dom.*; import java.util.*; import java.io.*; public class MySixthDomApp { private Writer out; private String local = ""; // define a hash table to store HTML markup // this hash table is used in the callback functions // for start, end and character elements ("priority" only) private Map StartElementHTML = new HashMap(); private Map EndElementHTML = new HashMap(); private Map PriorityHTML = new HashMap(); // constructor public MySixthDomApp (String xmlFile, Writer out) throws SAXException { this.out = out; // initialize StartElementHTML Hashmap StartElementHTML.put("todo","<ol>n"); StartElementHTML.put("item","<li>"); StartElementHTML.put("task","<b>"); StartElementHTML.put("due","&nbsp;<i>("); // initialize EndElementHTML Hashmap EndElementHTML.put("todo","</ol>n"); EndElementHTML.put("item","</font></li>n"); EndElementHTML.put("task","</b>"); EndElementHTML.put("due",")</i>"); // initialize PriorityHTML Hashmap PriorityHTML.put("1","<font face="Verdana" color="#ff0000" size="2">"); PriorityHTML.put("2","<font face="Verdana" color="#0000ff" size="2">"); PriorityHTML.put("3","<font face="Verdana" color="#000000" size="2">"); // create a Xerces DOM parser DOMParser parser = new DOMParser(); // parse the document try { parser.parse(xmlFile); Document document = parser.getDocument(); Element RootElement = document.getDocumentElement(); NodeList Children = RootElement.getChildNodes(); printData(Children); out.flush(); } catch (IOException e) { throw new SAXException(e); } } private void printData (NodeList NodeCollection) throws SAXException { try { // check if the node collection passed is NULL if (NodeCollection != null) { // iterate through the collection for (int i=0; i< NodeCollection.getLength(); i++) { // store the name of the element in a string. // this is used as a key into the HashMap. local = NodeCollection.item(i).getNodeName(); if(NodeCollection.item(i).getNodeType() == Node.ELEMENT_NODE) { // "priority" element needs special handling // for everything else... if(!((local.equals("priority")))) { // get the HTML markup String StartElement = StartElementHTML.get(local).toString(); String EndElement = EndElementHTML.get(local).toString(); // output starting HTML tags + content out.write(StartElement + GetElementData(NodeCollection.item(i))); // recursively traverse the tree printData(NodeCollection.item(i).getChildNodes()); // output ending HTML tags out.write(EndElement); } else { // handle the "priority" element differently. // get the data for the "priority" element String Priority = GetElementData(NodeCollection.item(i)); // use "priority" element value to get markup for the rest of the line out.write((PriorityHTML.get(Priority)).toString()); // move on printData(NodeCollection.item(i).getChildNodes()); } } } } } catch (IOException e) { throw new SAXException(e); } } private String GetElementData(Node parentNode) { // get node type int childType = parentNode.getFirstChild().getNodeType(); // return the node value if text node if (childType == Node.TEXT_NODE) { if(parentNode.getFirstChild().getNodeValue() != null) { return parentNode.getFirstChild().getNodeValue(); } } // else return null return null; } }
    This is much cleaner and easier to read than the previous example, since it uses Java's HashMap object to store key-value pairs mapping HTML markup to XML markup. Three HashMap's have been used here: StartElementHTML, which stores the HTML tags for opening XML elements; EndElementHTML, which stores the HTML tags for closing XML elements; and PriorityHTML, which stores the HTML tags for the "priority" elements defined for each "item".

    These HashMaps are populated with data in the class constructor:

    // initialize StartElementHTML Hashmap StartElementHTML.put("todo","<ol>n"); StartElementHTML.put("item","<li>"); StartElementHTML.put("task","<b>"); StartElementHTML.put("due","&nbsp;<i>("); // initialize EndElementHTML Hashmap EndElementHTML.put("todo","</ol>n"); EndElementHTML.put("item","</font></li>n"); EndElementHTML.put("task","</b>"); EndElementHTML.put("due",")</i>"); // initialize PriorityHTML Hashmap PriorityHTML.put("1","<font face="Verdana" color="#ff0000" size="2">"); PriorityHTML.put("2","<font face="Verdana" color="#0000ff" size="2">"); PriorityHTML.put("3","<font face="Verdana" color="#000000" size="2">");
    The rest of the constructor follows the pattern set previously - initialize parser, parse XML, get document element and first level children, and hand processing over to printData().

    The printData() function has also been modified to incorporate the HashMaps described above. Every time the function encounters an XML element, it uses the element name as a key into the HashMaps, retrieves the corresponding HTML markup for that element, and prints it. If the element is a "priority" element, there's no real data to be printed; rather, the formatting of the entire to-do item changes to reflect the item priority.

    private void printData (NodeList NodeCollection) throws SAXException { try { // check if the node collection passed is NULL if (NodeCollection != null) { // iterate through the collection for (int i=0; i< NodeCollection.getLength(); i++) { // store the name of the element in a string. // this is used as a key into the HashMap. local = NodeCollection.item(i).getNodeName(); if(NodeCollection.item(i).getNodeType() == Node.ELEMENT_NODE) { // "priority" element needs special handling // for everything else... if(!((local.equals("priority")))) { // get the HTML markup String StartElement = StartElementHTML.get(local).toString(); String EndElement = EndElementHTML.get(local).toString(); // output starting HTML tags + content out.write(StartElement + GetElementData(NodeCollection.item(i))); // recursively traverse the tree printData(NodeCollection.item(i).getChildNodes()); // output ending HTML tags out.write(EndElement); } else { // handle the "priority" element differently. // get the data for the "priority" element String Priority = GetElementData(NodeCollection.item(i)); // use "priority" element value to get markup for the rest of the line out.write((PriorityHTML.get(Priority)).toString()); // move on printData(NodeCollection.item(i).getChildNodes()); } } } } } catch (IOException e) { throw new SAXException(e); } }
    Here's the JSP code that uses the class above:

    <%@ page language="java" import="java.io.IOException" %> <html> <head> </head> <body> <h1><font face="Verdana">My Todo List</font></h1> <% try { MySixthDomApp mySixthExample = new MySixthDomApp("/www/xerces/WEB-INF/classes/todo.xml ",out); } catch (Exception e) { out.println("<font face="verdana" size="2">Something bad just happened: <br><b>" + e + "</b></font>"); } %> </body> </html>
    And here's what it all 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 4 hosted by Hostway