MSXML, continued - The InsertAfter() Function
(Page 4 of 5 )
The InsertAfter() function is called when the user specifies a CD in the XML document that comes before the new CD. Here’s the InsertAfter() function:
function InsertAfter(upc)
{
var objNewNode = LoadNewNode();
if(objNewNode == null)
{
return;
}
var root = objXML.documentElement;
var childNodes = root.childNodes;
for(var i=0; i < childNodes.length; i++)
{
var node = childNodes.item(i);
var nodeUPC = node.getAttribute("upc");
if(nodeUPC == upc)
{
root.insertBefore(objNewNode, childNodes.item(i+1));
document.all("xmlresult").value = objXML.xml;
return;
}
}
alert("Could not find node with upc " + upc);
}
The first three lines are the same as they are in previous functions.
The fourth line assigns the childNodes property of the IXMLDOMElement object to the childNodes variable. The childNodes property contains all the child nodes of the <catalog > element in the document.
The fifth line executes a for loop that steps through each child node looking for the child node whose upc attribute matches the UPC code that the user entered. The item() method is called to retrieve the node from the list. Next, the getAttribute() method is called and passed the name of the attribute whose value you want returned. And then an if statement is used to compare the value of the upc attribute of the current child node to the UPC that the user entered.
If they match, then the insertBefore() method is called to insert the new CD into the XML document. The insertBefore() method requires two arguments. The first argument references information about the new CD and the second argument references the existing node in the XML document. The second argument jumps one node ahead by using i+1. In this way, it’s going to the next node and inserting before that. The API does not have an insertAfter() method so this is another way to accomplish the same thing. Suppose you were inserting after the last node in the list. i+1 would not reference a valid node because it’s beyond the boundary of the list. The second argument would evaluate to null. When the method sees null as the second argument, it puts the new node last in the list. It’s equivalent to calling appendNode(). The XML document is then displayed before the function returns to the statement that called the InsertAfter() function.
If they don’t match, then the function returns without changing the XML document.
Here’s the XML document after the new CD is inserted:
<?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="75678367229">
<artist>Rush</artist>
<title>Rush in Rio</title>
<price>13.98</price>
<label>Atlantic</label>
<date>2003-10-21</date>
</cd>
<cd upc="75596280822">
<artist>Phish</artist>
<title>Live Phish, Vol. 15</title>
<price>26.99</price>
<label>ELEKTRA/WEA</label>
<date>2002-10-29</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: Create a New Element Programmatically >>
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.
|
|