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
Next: On accessing a structure >>
More ColdFusion Articles
More By Jayaram Krishnaswamy