Home > Articles

The Parser and DOM

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

DOM in Applications

Every XML application includes a parser. Why? Because every XML application needs to read XML files and reading XML files is what the parser is all about. Let's review a few examples.

Browsers

Obviously, the browser uses the DOM interface everywhere. DOM is not limited to XML islands; any document loaded in a browser is accessible through DOM.

Listings 7.8, 7.9, 7.10, and 7.11 show yet another version of the conversion utility. This version loads the XML document in one frame (so, it's not a Microsoft-specific XML island or Netscape's mixture of XHTML and XML; it's a regular document loaded in a frame) and loads the bulk of the utility in another frame.

Listing 7.8 shows the HTML file that creates the frames whereas Listing 7.9 implements the conversion utility. Listing 7.10 is the product list in XML. Notice that, unlike Listing 7.5, it does not use namespaces to work around a bug in Internet Explorer. It also applies a CSS style sheet that is available in Listing 7.11.

Listing 7.8: showparser.html

<html>
<head>
  <title>Currency Conversion</title>
</head>
<frameset cols="40%,60%">
  <frame src="controls.html" name="controls">
  <frame src="products3.xml" name="products">
</frameset>
</html>
<html>
<head>
  <title>Controls</title>
<script language="JavaScript">
function convert(form,document)
{
  var output = form.output,
    rate = form.rate.value;
  output.value = "";
  var root = document.documentElement;
  walkNode(root,output,rate)
}

function walkNode(node,output,rate)
{
 if(node.nodeType == 1)
 {
  if(node.nodeName == "product")
   walkProduct(node,output,rate);
  else
     {
   var children,
     i;
   children = node.childNodes;
   for(i = 0;i < children.length;i++)
    walkNode(children.item(i),output,rate);
  }
 }
}

function walkProduct(node,output,rate)
{
 var children = node.childNodes,
   i;
 for(i = 0;i < children.length;i++)
 {
  var child = children.item(i);
  if(child.nodeType == 1)
  {
   if(child.nodeName == "price")
    walkPrice(child,output,rate);
   else if(child.nodeName == "name")
    walkName(child,output);
  }
 }
 output.value += "\r";
}

function walkPrice(node,output,rate)
{
 var children = node.childNodes,
   price = "";
 for(i = 0;i < children.length;i++)
 {
  var child = children.item(i);
  if(child.nodeType == 3)
    price += child.data;
 }
 output.value += price * rate;
}

function walkName(node,output,rate)
{
 var children = node.childNodes;
 for(i = 0;i < children.length;i++)
 {
  var child = children.item(i);
  if(child.nodeType == 3)
    output.value += child.data;
 }
 output.value += ": ";
}
</script>
</head>
<body>
<center>
<form id="controls">
Rate: <input type="text" name="rate" value="1.0622" size="5"><br>
<input type="button" value="Convert"
    onclick="convert(controls,parent.products.document)">
<input type="button" value="Clear" onclick="output.value=''"><br>
<!-- make sure there is one character in the text area -->
<textarea name="output" rows="10" cols="30" readonly> </textarea>
</form>
</center>
</body>
</html>

Listing 7.10: products3.xml

<?xml version="1.0"?>
<?xml-stylesheet href="products3.css" type="text/css"?>
<products>
  <product>
   <name>XML Editor</name>
   <price>499.00</price>
  </product>
  <product>
   <name>DTD Editor</name>
   <price>199.00</price>
  </product>
  <product>
   <name>XML Book</name>
   <price>19.99</price>
  </product>
  <product>
   <name>XML Training</name>
   <price>699.00</price>
  </product>
</products>

Listing 7.11: products3.css

product
{
  display: block;
  font-family: Palatino, Garamond, "Times New Roman", serif;
}

name
{
  font-weight: bold;
}

Figure 7.10 shows the result in a browser. Note that this code is compatible with both Internet Explorer 5 and Netscape 6.

The code is familiar; for the most part it's copied verbatim from Listing 7.3. The DOM interface gives direct access to the content of the XML frame:

<input type="button" value="Convert"
    onclick="convert(controls,parent.products.document)">
Figure 7.10: The result in a browser.

Editors

XML editors also use DOM. For example, XMetaL from SoftQuad exposes the document being edited through DOM.

For example, macros can access the document to create tables of contents, indexes, and so on. Using macros and DOM, you can customize the editor to suit your needs.

Databases

An XML database stores XML documents in binary format. It is therefore faster to load and manipulate documents.

Such a database exposes its documents to applications using DOM. The application does not even know it is working against a database. Through DOM, it makes no difference whether the document is in a database or in an XML file.

If you would like to experiment with this feature, you can download the GMD-IPSI PDOM engine from xml.darmstadt.gmd.de/xql. The engine implements Persistent DOM (PDOM), which is an interface that stores XML documents in binary format. The interface to access the binary document is familiar DOM, which means that any application that works with XML files can be upgraded to binary files with little or no work.

What's Next

This chapter looked at an object-based interface for XML parsers. In the next chapter, you will look at an event-based interface: SAX. It is interesting to compare SAX and DOM.

In Chapter 9, you will learn how to create XML documents. This is the opposite of parsers, but there, again, DOM can be useful.

  • + Share This
  • 🔖 Save To Your Account