XML
  Home arrow XML arrow Page 2 - MSXML, concluded
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 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
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, concluded
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2006-03-30

    Table of Contents:
  • MSXML, concluded
  • The DisplayTitles() Function
  • The ValidateDocument() Function
  • MSXML and XSLT
  • Summary

  • 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
     
    Iron Speed
     
    ADVERTISEMENT

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    MSXML, concluded - The DisplayTitles() Function


    (Page 2 of 5 )

    You use the DisplayTitles() function to copy and display information contained in an XML document, but not alter the original document. Here’s the DisplayTitles() function:

    function DisplayTitles()
    {
       var result = "";
       var objNodes = objXML.selectNodes("/catalog/cd/title");
       for(var i=0; i < objNodes.length; i++)
       {
         
    result += objNodes.item(i).text + "\r\n";
       }
       document.all("xmlresult").value = result;
    }

    You’ll notice that the DisplayTitles() function has many components that are found in previous examples shown in this chapter. And although we’re retrieving selected titles, you can use the same code to select any element from the XML document by simply replacing the element title with the appropriate element name.

    The first line declares a variable. The pair of double quotations indicates an empty string is assigned to the variable to initialize it.

    The second line calls the selectNodes() function to retrieve a collection that contains title elements.

    The third line steps through the collection and assigns the text value of these elements to the result variable. Notice that it also assigns a \r\n. The \r is a carriage return and the \n is a new line. This simply places each element on its own line when the results are displayed.

    The fourth line displays the text of the elements as shown here:

    How to Dismantle an Atomic Bomb

    Physical Graffiti

    Rush in Rio

    Songs in the Attic

    Houses of the Holy

    Are You Experienced?

    The Times They Are A-Changin’

    The DeleteNodes() Function

    The DeleteNodes() function removes a specific node from the XML document. Here’s the DeleteNodes() function. You’ll notice that it requires one argument, which is the UPC code of the CD that is to be deleted from the XML document.

    function DeleteNodes(upc)
    {
       var objNodes = objXML.selectNodes(
           
    "/catalog/cd[@upc='" + upc + "']"); 
       if(objNodes.length == 0)
       {
         
    alert("Could not find node with upc " + upc);
         
    return;
       }
       for(var i=0; i < objNodes.length; i++)
       {
         
    objXML.documentElement.removeChild(objNodes.item(i));
       }
       document.all("xmlresult").value = objXML.xml;
    }

    The first line calls the selectNodes() method and passes it the XPath expression that’s used to locate elements whose upc attribute matches the CD UPC the user enters. The selectNodes() method returns a collection containing those elements.

    The second line uses an if statement to evaluate if a CD matched the UPC. If not, then the length property is zero, the alert message is displayed, and the function returns without deleting any information from the XML document.

    The third line executes only if there are elements to be deleted. It uses a for loop to step through the collection and calls the removeChild() method to remove the element.

    The fourth line executes once the final element is deleted. This line displays the results 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="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>

    More XML Articles
    More By McGraw-Hill/Osborne


       · 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 5 hosted by Hostway