XML
  Home arrow XML arrow Page 5 - MSXML, continued
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

MSXML, continued
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2006-03-23

    Table of Contents:
  • MSXML, continued
  • The InsertLast() Method
  • The InsertBefore() Function
  • The InsertAfter() Function
  • Create a New Element Programmatically

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    MSXML, continued - Create a New Element Programmatically


    (Page 5 of 5 )

    Now we’ll show you how to create a new <cd> element and its child elements and insert them into an XML document using a program. First you’ll create an HTML page that contains the input fields where you can enter values for the new CD:

    <tr valign="top">
       <td nowrap><a href="#" onclick="CreateAndAppendNode();
            return false">Create/Append Node</a></td>
       <td nowrap>
          upc: <input type="text" id="createUpc"
              value="75596280822" size="15"><br>
          artist: <input type="text" id="createArtist"
             value="Phish" size="15"><br>
          title: <input type="text" id="createTitle"
             
    value="Live Phish, Vol. 15" size="15"><br>
       price: <input type="text" id="createPrice"
             value="26.99" size="15"><br>
       label: <input type="text" id="createLabel"
             value="ELEKTRA/WEA" size="15"><br>
       date: <input type="text" id="createDate" 
             value="2002-10-29" size="15">
       </td>
    </tr>

    You’re required to enter six values. These are the upc attribute and values for each of the five child elements. Click the hyperlink once you’re finished and the CreateAndAppendNode() function executes. Here’s the CreateAndAppendNode() function:

    function CreateAndAppendNode()
    {
       var upc = document.all("createUpc").value;
       var artist = document.all("createArtist").value;
       var title = document.all("createTitle").value;
       var price = document.all("createPrice").value;
       var label = document.all("createLabel").value;
       var date = document.all("createDate").value;
      
    var elementCd = objXML.createElement("cd");
       elementCd.setAttribute("upc", upc);
       var elementArtist = objXML.createElement("artist");
       var textArtist = objXML.createTextNode(artist);
       elementArtist.appendChild(textArtist);
       elementCd.appendChild(elementArtist);

       var elementTitle = objXML.createElement("title");
       var textTitle = objXML.createTextNode(title);
       elementTitle.appendChild(textTitle);
       elementCd.appendChild(elementTitle);
      
    var elementPrice = objXML.createElement("price");
       var textPrice = objXML.createTextNode(price);
       elementPrice.appendChild(textPrice);
       elementCd.appendChild(elementPrice);
       var elementLabel = objXML.createElement("label");
       var textLabel = objXML.createTextNode(label);
       elementLabel.appendChild(textLabel);

       elementCd.appendChild(elementLabel);
      
    var elementDate = objXML.createElement("date");
       var textDate = objXML.createTextNode(date);
       elementDate.appendChild(textDate);
       elementCd.appendChild(elementDate);
       var root = objXML.documentElement;
       root.appendChild(elementCd);

       document.all("xmlresult").value = objXML.xml;
    }

    The first six lines gather values from the HTML table and assign them to variables.

    Line seven calls the createElement() method to create a new element. The createElement() method requires one argument, which is the name of the element that you want to create. In this example, you’re creating a cd element. The createElement() method returns a reference to the new element.

    Line eight calls the setAttribute() method to assign a value to the attribute of the new element. The setAttribute() method requires two arguments. The first argument is the name of the attribute that’s being set and the second argument is the value assigned to the new attribute.

    Lines 9 through 30 create child elements for the <cd> element. Notice that each child element is actually two nodes—one for the element and the other for the text. Line 31 displays the XML document, as 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="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>
      
    <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>
    </catalog>

    Please check back next week for the conclusion of this article. 


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This article is an excerpt from the book "XML DeMYSTified," published by...
     

    Buy this book now. 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.

    XML ARTICLES

    - More on Triggers and Styles and Control Temp...
    - Looking at Triggers with Styles and Control ...
    - A Closer Look at Styles and Control Templates
    - Styles and Control Templates
    - Properties and More in XAML
    - Elements and Attributes in XAML
    - XAML in a Nutshell
    - Importing XML Files into Access 2007
    - Using MSXML3.0 with VB 6.0
    - MSXML, concluded
    - MSXML, continued
    - MSXML Tutorial
    - Generating XML Schema Dynamically Using VB.N...
    - XSL Transformations using ASP.NET
    - Applying XSLT to XML Using ASP.NET





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT