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.
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.
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.
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.
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