MSXML, continued - The InsertBefore() Function
(Page 3 of 5 )
The InsertBefore() function is called when the user specifies the position of the new CD in the XML document. The user does this by entering the UPC code of the CD that will come after the new CD in the XML document. Here’s the InsertBefore() function:
function InsertBefore(upc)
{
var objNewNode = LoadNewNode();
if(objNewNode == null)
{
return;
}
var root = objXML.documentElement;
var objNodes = objXML.selectNodes(
"/catalog/cd[@upc='" + upc + "']");
if(objNodes.length == 0)
{
alert("Could not find node with upc " + upc);
return;
}
root.insertBefore(objNewNode, objNodes.item(0));
document.all("xmlresult").value = objXML.xml;
}
The UPC of the CD that will come after the new CD in the XML document is passed as an argument to the InsertBefore() function by the statement that calls the InsertBefore() function (see the “Getting Down and Dirty with MSXML” section in this chapter).
The first four lines of the InsertBefore() function are the same as those for the InsertFirst() and InsertLast() functions.
Line five calls the selectNodes() method of the DOMDocument object. This method requires one argument containing an XPath expression (see Chapter 5) to identify the node that will come after the new CD in the XML document.
This expression says, Look in the /catalog element for a cd element whose upc attribute is equal to the UPC passed to the selectNodes() method. There can be more than one match. Therefore, the selectNodes() method returns a collection that contains references of matching nodes.
Line six evaluates the value of the length property of the node list returned by the selectNodes() method. If the length is zero, then the CD entered by the user can’t be located in the XML document and an alert message is displayed; then the function returns to the statement that called it.
Line seven executes if the selectNodes() method returned a node indicating that the CD was found in the XML document. Line seven calls the insertBefore() method, which is also called by the InsertFirst() function and InsertLast() function. The insertBefore() method requires two arguments. The first argument references the new CD and the second argument references the first CD that will come after the new CD in the XML document. The second argument is the first node the collection returned by the selectNodes() method.
Line eight is the same as it was in the previous functions.
Here’s the XML document after the InsertBefore() function executes:
<?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="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="75678367229">
<artist>Rush</artist>
<title>Rush in Rio</title>
<price>13.98</price>
<label>Atlantic</label>
<date>2003-10-21</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 InsertAfter() 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.
|
|