MSXML, continued
(Page 1 of 5 )
This article, the second 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).
The InsertFirst() Method
The InsertFirst() method is called when the user decides to place information about the new CD at the beginning of the XML document. Here’s the InsertFirst() method:
function InsertFirst()
{
var objNewNode = LoadNewNode();
if(objNewNode == null)
{
return;
}
var root = objXML.documentElement;
root.insertBefore(objNewNode, root.firstChild);
document.all("xmlresult").value = objXML.xml;
}
The first line calls the LoadNewNode() method, which returns a reference to the root node of the information about the new CD. The reference is assigned to the objNewNode variable.
The second line determines if the value of the objNewNode is null. It’s null if the LoadNewNode() method doesn’t return a reference to the root node. If this happens, then the InsertFirst() method returns without inserting information about the new CD at the beginning of the XML document.
The third line is executed if the LoadNewNode() method returns a root node. The root node is a reference to an IXMLDOMElement object. This line assigns the value of the IXMLDOMElement object’s documentElement property of the new CD information to a variable called root.
The fourth line calls the insertBefore() method of the IXMLDOMElement object. The insertBefore() method has two arguments. The first argument is a reference to the node that’s being inserted into the document. This reference is returned by the LoadNewNode() method. The second argument is the node that will come after the new CD in the XML document.
The first CD in the XML document is 602498678299 (see the “Getting Down and Dirty with MSXML” section earlier in this chapter). The new CD will be inserted before CD 602498678299, making the new CD appear first in the XML document and CD 602498678299 second.
The second argument to the insertBefore() method is reference to CD 602498678299. CD 602498678299 is first in the XML document and, therefore, it can be identified by using the firstChild property of the IXMLDOMElement object.
The fifth line displays the code output of the XML representation of the DOMDocument into the text area of the HTML form. The output looks something like this:
<?xml version="1.0"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<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="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="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 InsertLast() Method >>
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.
|
|