Home arrow ColdFusion arrow Page 2 - CFXML: Probing XMLDOM in ColdFusion
COLDFUSION

CFXML: Probing XMLDOM in ColdFusion


This tutorial examines how to access the individual elements of an XML document in ColdFusion, if you have a reference to that document. Two ways to access the individual elements are demonstrated.

Author Info:
By: Jayaram Krishnaswamy
Rating: 5 stars5 stars5 stars5 stars5 stars / 16
April 03, 2006
TABLE OF CONTENTS:
  1. · CFXML: Probing XMLDOM in ColdFusion
  2. · Create a ColdFusion Object Using the Microsoft.XMLDOM Class
  3. · On accessing a structure
  4. · How do we get to the attributes?
  5. · How do we get at the Comment node?

print this article
SEARCH DEVARTICLES

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 

blog comments powered by Disqus
COLDFUSION ARTICLES

- Adobe ColdFusion Just Got More RAD
- 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...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials