ColdFusion
  Home arrow ColdFusion arrow Page 2 - CFXML: Probing XMLDOM in ColdFusion
IBM developerWorks
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
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? 
COLDFUSION

CFXML: Probing XMLDOM in ColdFusion
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2006-04-03

    Table of Contents:
  • CFXML: Probing XMLDOM in ColdFusion
  • Create a ColdFusion Object Using the Microsoft.XMLDOM Class
  • On accessing a structure
  • How do we get to the attributes?
  • How do we get at the Comment node?

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    CFXML: Probing XMLDOM in ColdFusion - Create a ColdFusion Object Using the Microsoft.XMLDOM Class


    (Page 2 of 5 )

    In this case we will be using ColdFusion's <cfobject..> tag to create a reference to the XMLDOM. According to Macromedia, the <cfobject..> tag can be used to access methods that are related to COM, JAVA, and CORBA of similarly named type objects. For each type there are couple of other attributes, which in the case of type=COM are Action, Class, Name, Context, and Server. 

    Assuming that we place the webstudents.xml file in a virtual directory on the localhost, we can create an object in ColdFusion using the <cfobject..> tag by using the create method of the tag shown in the following snippet.

    <cfobject type="COM"
    name="objXMLDOM"
    class="Microsoft.XMLDOM"
    action="CREATE"> 
    <cfset objXMLDOM.async = false>
    <CFSET objXMLDOM.LOAD("http://localhost/webstudents.xml")> 
    Accessing the individual parts

    Once you have a reference to this object, then accessing the properties will be similar to the way they were accessed in the tutorial Roaming through XMLDOM: an Ajax Prerequisite. I am describing two examples taken as is from the above tutorial modified for ColdFusion. For example the first and lastChild elements are accessed by the following snippet, firstlast.cfm.

    <cfobject type="COM"
    name="objXMLDOM"
    class="Microsoft.XMLDOM"
    action="CREATE"> 
    <cfset objXMLDOM.async = false>
    <CFSET objXMLDOM.LOAD("http://localhost/webstudents.xml")> 
    <cfset doct=objXMLDOM.documentElement.firstChild.nodeName>
    <cfoutput>
    #doct#
    </cfoutput>
    <br/>
    <cfset doct=objXMLDOM.documentElement.lastChild.nodeName>
    <cfoutput>
    #doct#
    </cfoutput>
    

    This cfm file would display the following:

    #comment
    student
    

    As a second example, you can interrogate the various items that are in the student node by the following snippet, AllStudents.cfm.

    <cfobject type="COM"
    name="objXMLDOM"
    class="Microsoft.XMLDOM"
    action="CREATE"> 
    <cfset objXMLDOM.async = false>
    <CFSET objXMLDOM.LOAD("http://localhost/webstudents.xml")> 
    <cfloop index="i" from ="1" to="4">
    <cfoutput >
    #objXMLDOM.documentElement.childNodes.item(i).text#<br/>
    </cfoutput>
    </cfloop>
    

    This would produce the following display in the browser:

    Linda Jones Access, VB5.0
    Adam Davidson Cobol, MainFrame
    Charles Boyer HTML, Photoshop
    Charles Mann Cobol, MainFrame
    Accessing an XML file with CF

    It is also possible to get a handle on the XML Document using the <Cffile.....> tag. Using this method the XML functions supported by ColdFusion can be used. The following snippet shows how to access the same webstudents.xml file using this method.

    <cffile action="read" file="C:\Inetpub\wwwroot\webstudents.xml"
    variable="xmldoc">
    <cfset mydoc = XmlParse(xmldoc)>
    <cfoutput>
    mydoc.XMLName= 
    #mydoc.XMLName#
    <br/>
    mydoc.XMLType=
    #mydoc.XMLType#
    <br/>
    mydoc.XMLRoot.XMLName=
    #mydoc.XMLRoot.XMLName#
    <br/>
    mydoc.XMLRoot.XMLType=
    #mydoc.XMLRoot.XMLType#
    <br/>
    mydoc.XMLRoot.XMLNodes[1].XMLType=
    #mydoc.XMLRoot.XMLNodes[1].XMLType#
    <br/>
    mydoc.XMLRoot.XMLNodes[1].XMLName=
    #mydoc.XMLRoot.XMLNodes[1].XMLName#
    <br/>
    mydoc.XMLRoot.student.XMLType=
    #mydoc.XMLRoot.student.XMLType#
    <br/>
    </cfoutput>
    

    The browser display for the above will be:

    mydoc.XMLName= #document 
    mydoc.XMLType= DOCUMENT 
    mydoc.XMLRoot.XMLName= wclass 
    mydoc.XMLRoot.XMLType= ELEMENT 
    mydoc.XMLRoot.XMLNodes[1].XMLType= TEXT 
    mydoc.XMLRoot.XMLNodes[1].XMLName= #text 
    mydoc.XMLRoot.student.XMLType= ELEMENT 

    More ColdFusion Articles
    More By Jayaram Krishnaswamy


       · The tutorial discusss two ways to access an XML document using ColdFusion. The...
     

    COLDFUSION ARTICLES

    - How to Access a SQL Anywhere Database with C...
    - CFXML: Probing XMLDOM in ColdFusion
    - Creating a Web Service with ColdFusion: the ...
    - CFAjax: What it is and How to Use it
    - Querying SQL 2000 Server from ColdFusion
    - Introduction to ColdFusion Markup Language, ...
    - Introduction to ColdFusion Markup Language
    - Databases and Dreamweaver MX 2004, concluded
    - Databases and Dreamweaver MX 2004
    - Welcome to Coldfusion MX 6.1, concluded
    - Welcome to Coldfusion MX 6.1
    - What You Must Know About ColdFusion Flow-Con...
    - What You Must Know About Operators in ColdFu...
    - Everything You Must Know About ColdFusion Va...
    - My First Application on ColdFusion MX Server


    Iron Speed





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway