MSXML, concluded - The DisplayTitles() Function
(Page 2 of 5 )
You use the DisplayTitles() function to copy and display information contained in an XML document, but not alter the original document. Here’s the DisplayTitles() function:
function DisplayTitles()
{
var result = "";
var objNodes = objXML.selectNodes("/catalog/cd/title");
for(var i=0; i < objNodes.length; i++)
{
result += objNodes.item(i).text + "\r\n";
}
document.all("xmlresult").value = result;
}
You’ll notice that the DisplayTitles() function has many components that are found in previous examples shown in this chapter. And although we’re retrieving selected titles, you can use the same code to select any element from the XML document by simply replacing the element title with the appropriate element name.
The first line declares a variable. The pair of double quotations indicates an empty string is assigned to the variable to initialize it.
The second line calls the selectNodes() function to retrieve a collection that contains title elements.
The third line steps through the collection and assigns the text value of these elements to the result variable. Notice that it also assigns a \r\n. The \r is a carriage return and the \n is a new line. This simply places each element on its own line when the results are displayed.
The fourth line displays the text of the elements as shown here:
How to Dismantle an Atomic Bomb
Physical Graffiti
Rush in Rio
Songs in the Attic
Houses of the Holy
Are You Experienced?
The Times They Are A-Changin’
The DeleteNodes() Function
The DeleteNodes() function removes a specific node from the XML document. Here’s the DeleteNodes() function. You’ll notice that it requires one argument, which is the UPC code of the CD that is to be deleted from the XML document.
function DeleteNodes(upc)
{
var objNodes = objXML.selectNodes(
"/catalog/cd[@upc='" + upc + "']");
if(objNodes.length == 0)
{
alert("Could not find node with upc " + upc);
return;
}
for(var i=0; i < objNodes.length; i++)
{
objXML.documentElement.removeChild(objNodes.item(i));
}
document.all("xmlresult").value = objXML.xml;
}
The first line calls the selectNodes() method and passes it the XPath expression that’s used to locate elements whose upc attribute matches the CD UPC the user enters. The selectNodes() method returns a collection containing those elements.
The second line uses an if statement to evaluate if a CD matched the UPC. If not, then the length property is zero, the alert message is displayed, and the function returns without deleting any information from the XML document.
The third line executes only if there are elements to be deleted. It uses a for loop to step through the collection and calls the removeChild() method to remove the element.
The fourth line executes once the final element is deleted. This line displays the results we show here:
<?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>
<cd upc="75679244222">
<artist>Led Zeppelin</artist>
<title>Physical Graffiti</title>
<price>22.99</price>
<label>Atlantic</label>
<date>1994-08-16</date>
</cd>
<cd upc="74646938720">
<artist>Billy Joel</artist>
<title>Songs in the Attic</title>
<price>10.99</price>
<label>Sony</label>
<date>1998-10-20</date>
</cd>
<cd upc="75678263927">
<artist>Led Zeppelin</artist>
<title>Houses of the Holy</title>
<price>10.98</price>
<label>Atlantic</label>
<date>1994-07-19</date>
</cd>
<cd upc="8811160227">
<artist>Jimi Hendrix</artist>
<title>Are You Experienced?</title>
<price>12.99</price>
<label>Experience Hendrix</label>
<date>1997-04-22</date>
</cd>
<cd upc="74640890529">
<artist>Bob Dylan</artist>
<title>The Times They Are A-Changin'</title>
<price>9.99</price>
<label>Sony</label>
<date>1990-10-25</date>
</cd>
</catalog>
Next: The ValidateDocument() 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.
|
|