XML
  Home arrow XML arrow 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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
     
     
    ADVERTISEMENT


    MSXML, concluded


    (Page 1 of 5 )

    This article, the third 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).

    Select, Extract, Delete, and Validate

    So far in this chapter, you’ve learned how to insert one XML document into another. In addition to this, you’ll need to select, extract, delete, and validate information contained in an XML document.

    We’ll explore how to perform these common tasks in this section. First we’ll show you how to create an HTML page that enables you to execute each of these tasks. In a real-world application, of course, these tasks would be built into your application.

    Here’s the HTML page we’ll use for these examples:

    <tr valign="top">
    <td colspan="2" nowrap>
    <a href="#" onclick="SelectArtist(document.all('artist').value);
       return false;">Select Artist:</a>
       <input type="text" id="artist" value="U2" size="15"><br>
    <a href="#" onclick="DisplayTitles();
        return false;">Display Titles</a><br>
    <a href="#" onclick="DeleteNodes(
        document.all('upcDelete').value); return
        false;">Delete Nodes w/UPC:</a>
      
    <input type="text" id="upcDelete"
            value="75678367229" size="15"><br>
    <a href="#" onclick="ValidateDocument();
           
    return false;">Validate Document</a>
    </td>
    </tr>

    The SelectArtist() Function— Filtering an XML Document

    The SelectArtist() function is used to display information about an artist’s CDs by entering the name of the artist and then having the SelectArtist() search and display related information about the artist’s CDs. Here’s the SelectArtist() function:

    function SelectArtist(artist)
    {
       var objNodes = objXML.selectNodes(
            
    "/catalog/cd[artist='" + artist + "']")
       if(objNodes.length == 0)
       {
         
    alert("Could not find artist with name " + artist);
          
    return;
       }
       var root = objXML.documentElement;
       var cdList = root.selectNodes("/catalog/cd");
       cdList.removeAll();
       for(var i=0; i < objNodes.length; i++)
       {
         
    root.appendChild(objNodes.item(i));
       }
       document.all("xmlresult").value = objXML.xml;
    }

    The first line calls the selectNodes() method, which you learned about throughout this chapter. The selectNodes() method requires one argument, which is the XPath expression (see Chapter 5) used to identify the artist. This expression says, Look in the catalog element for a cd element whose artist is equal to the artist entered by the user. The selectNodes() method returns a collection that contains information about all the CDs that are listed for the artist.

    The second line examines the length property of the collection, which contains the total number of items returned by the selectNodes() method. If the length is zero, then the artist wasn’t found. An alert is displayed on the screen that the function returns without displaying any information.

    Line three executes if the length is greater than zero, and assigns reference to the documentElement to the root variable.

    Line four calls the selectNodes() method to retrieve information about all the CDs in the document.

    Line five calls the removeAll() method, which removes all information about CDs from the XML document. The XML document now looks like this:

    <?xml version="1.0"?>
    <!DOCTYPE catalog SYSTEM "catalog.dtd"> <catalog></catalog>

    Line six executes a for loop that calls the appendChild node to insert back into the XML document information about CDs from the selected artist. The XML document now looks like this:

    <?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>
    </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-2010 by Developer Shed. All rights reserved. DS Cluster 10 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek