MSXML Tutorial - Adding a New Element
(Page 4 of 5 )
The XML document contains information about CDs. Each CD has a upc attribute and five child elements. Add the following CD to the 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>
You’ll need to use four different functions to determine where to place the new CD within the XML document. These functions are
Each function is called by an option on the HTML form. Options appear in the first column of the table. The user of the application decides the position of the new CD within the XML document by selecting the appropriate option.
The first two options place the new CD at the beginning or at the end of the XML document, respectively. The last two options require the user to specify a UPC. The UPC is the identifier for a CD that’s already in the XML document. The function then places the new CD either before or after the CD that the user specifies.
The second column contains a text area containing information about the new CD. We’ve provided a default value when the page loads, but you can change this in the browser. Each function references the text area value when inserting the new CD into the XML document.
<tr valign="top">
<td nowrap>
<a href="#" onclick="InsertFirst();
return false;">Insert First:</a><br>
<a href="#" onclick="InsertLast();
return false;">Insert Last:</a><br>
<a href="#" onclick="InsertBefore(document.all('upcBefore').value);
return false;">Insert Before UPC:</a>
<input type="text" id="upcBefore"
value="75678367229" size="15"><br>
<a href="#" onclick="InsertAfter(document.all('upcAfter').value);
return false;">Insert After UPC:</a>
<input type="text" id="upcAfter"
value="75678367229" size="15"><br>
</td>
<td><textarea id="newnode" rows="10" cols="80">
<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>
</textarea>
</td>
</tr>
Next: The LoadNewNode() 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.
|
|