MSXML, concluded (Page 1 of 5 )
This article, the third of three parts, explains what MSXML is and how to access an XML document using JavaScript. It is excerpted from chapter 10 of
XML DeMYSTified, written by Jim Keogh and Ken Davidson (McGraw-Hill/Osborne, 2005; ISBN: 0072262109).
Select, Extract, Delete, and Validate
So far in this chapter, you’ve learned how to insert one XML document into another. In addition to this, you’ll need to select, extract, delete, and validate information contained in an XML document.
We’ll explore how to perform these common tasks in this section. First we’ll show you how to create an HTML page that enables you to execute each of these tasks. In a real-world application, of course, these tasks would be built into your application.
Here’s the HTML page we’ll use for these examples:
<tr valign="top">
<td colspan="2" nowrap>
<a href="#" onclick="SelectArtist(document.all('artist').value);
return false;">Select Artist:</a>
<input type="text" id="artist" value="U2" size="15"><br>
<a href="#" onclick="DisplayTitles();
return false;">Display Titles</a><br>
<a href="#" onclick="DeleteNodes(
document.all('upcDelete').value); return
false;">Delete Nodes w/UPC:</a>
<input type="text" id="upcDelete"
value="75678367229" size="15"><br>
<a href="#" onclick="ValidateDocument();
return false;">Validate Document</a>
</td>
</tr>
The SelectArtist() Function— Filtering an XML Document
The SelectArtist() function is used to display information about an artist’s CDs by entering the name of the artist and then having the SelectArtist() search and display related information about the artist’s CDs. Here’s the SelectArtist() function:
function SelectArtist(artist)
{
var objNodes = objXML.selectNodes(
"/catalog/cd[artist='" + artist + "']")
if(objNodes.length == 0)
{
alert("Could not find artist with name " + artist);
return;
}
var root = objXML.documentElement;
var cdList = root.selectNodes("/catalog/cd");
cdList.removeAll();
for(var i=0; i < objNodes.length; i++)
{
root.appendChild(objNodes.item(i));
}
document.all("xmlresult").value = objXML.xml;
}
The first line calls the selectNodes() method, which you learned about throughout this chapter. The selectNodes() method requires one argument, which is the XPath expression (see Chapter 5) used to identify the artist. This expression says, Look in the catalog element for a cd element whose artist is equal to the artist entered by the user. The selectNodes() method returns a collection that contains information about all the CDs that are listed for the artist.
The second line examines the length property of the collection, which contains the total number of items returned by the selectNodes() method. If the length is zero, then the artist wasn’t found. An alert is displayed on the screen that the function returns without displaying any information.
Line three executes if the length is greater than zero, and assigns reference to the documentElement to the root variable.
Line four calls the selectNodes() method to retrieve information about all the CDs in the document.
Line five calls the removeAll() method, which removes all information about CDs from the XML document. The XML document now looks like this:
<?xml version="1.0"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd"> <catalog></catalog>
Line six executes a for loop that calls the appendChild node to insert back into the XML document information about CDs from the selected artist. The XML document now looks like this:
<?xml version="1.0"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd"> <catalog>
<cd upc="602498678299">
<artist>U2</artist>
<title>How to Dismantle an Atomic Bomb</title>
<price>13.98</price>
<label>Interscope Records</label>
<date>2004-11-23</date>
</cd>
</catalog>
Next: The DisplayTitles() Function >>
More XML Articles
More By McGraw-Hill/Osborne
|
This article is excerpted from chapter 10 of XML DeMYSTified, written by Jim Keogh and Ken Davidson (McGraw-Hill/Osborne, 2005; ISBN: 0072262109). Check it out today at your favorite bookstore. Buy this book now.
|
|