|
Reading XML using DOMDocument Object in Cold Fusion |
There are many ways we can read the xml document in cold fusion. Cold Fusion provides different tags to do the xml data manipulation but if we use the Microsoft XML Dom object, the xml document reading is much easier. I would like to show you an example in this article about reading an xml document and storing its information in a query object for further use in the cold fusion application. In some scenarios we might not store our data in database but keep them in XML Document. In such a scenario we need to find a way to read data in cold fusion. Using <cfobject tag we can call the Microsoft Dom object in cold fusion.
Using <cfobject tag..
<cfobject is used to call COM, CORBA, and JAVA objects. This tag allows initializing the method and properties of COM object. The action attribute of <cfobject tag allows to CREATE or CONNECT to the com object in the server.
First let us take look at the xml data we are going to read using DOM Document COM object in cold fusion. The xml data contains the CD catalogues with 4 nodes.
cd_catalog.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<PRICE>10.90</PRICE>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<PRICE>9.90</PRICE>
</CD>
<CD>
<TITLE>Greatest Hits</TITLE>
<ARTIST>Dolly Parton</ARTIST>
<PRICE>9.90</PRICE>
</CD>
<CD>
<TITLE>Still got the blues</TITLE>
<ARTIST>Gary Moore</ARTIST>
<PRICE>10.20</PRICE>
</CD>
</CATALOG>
Read the XML file contents
<cffile is used here for reading cd_catalog.xml and store the content to a variable.
Next step is loading this Xml string variable using DOM object. If the object couldn’t find any child nodes, it throws an error other wise it reads the node elements by tag name provided in the function. Then loop through the XmlNode collection and create a new row in the cfquery object for further use which we re-query in cold fusion as any other database query.
readxml.cfm
<!--- intialize the DOM object first --->
<cfobject type="COM" name="xmlDoc" class="MSXML2.DOMDocument.4.0" action="CREATE">
<cfset xmlDoc.async = false>
<cffile action="read" file="#ExpandPath('cd_catalog.xml')#" variable="XmlString">
<!--- load the xml file string to DOM object --->
<cfset xmlDoc.loadXML("#XmlString#")>
<!--- check whether any child nodes available for this xml document --->
<cfif not xmlDoc.hasChildNodes()>
<cfoutput>XML document is empty. Please process a valid xml file.</cfoutput> <cfelse>
<!--- read the XML NODE --->
<cfset colCDS = xmlDoc.getElementsByTagName("CD")>
<!--- total number of CD elements --->
<cfset lclSize=colCDS.length>
<!--- create a query at run time and insert all the xml node values to it --->
<cfset qryCdCatalog=QueryNew("id,title,artist,price")>
<cfset temp=QueryAddrow(qryCdCatalog,lclSize)>
<!--- row number variable --->
<cfset x=1>
<!--- loop through the CD collection object --->
<cfloop collection="#colCDS#" item="thisCD">
<!--- get the current CD object's attributes --->
<cfset cdChildNodes = thisCD.childNodes>
<!--- insert values in to a query object, first field is the increment id--->
<cfset temp=QuerySetCell(qryCdCatalog,"id",x,x)>
<!--- loop through the attribute list --->
<cfloop collection="#cdChildNodes#" item="thisNode">
<!--- insert attributes based on node name --->
<cfswitch expression="#thisNode.nodename#">
<cfcase value="TITLE">
<!--- insert title --->
<cfset temp=QuerySetCell(qryCdCatalog,"title","#thisNode.text#",x)>
</cfcase>
<cfcase value="ARTIST">
<!--- insert artist --->
<cfset temp=QuerySetCell(qryCdCatalog,"artist","#thisNode.text#",x)>
</cfcase>
<cfcase value="PRICE">
<!--- insert price --->
<cfset temp=QuerySetCell(qryCdCatalog,"price","#thisNode.text#",x)>
</cfcase>
</cfswitch>
</cfloop>
<!--- increment the row id here --->
<cfset x= incrementvalue(x)>
</cfloop>
<!--- you may use a query of query to filter the xml values --->
<cfquery name="qry_getcdcatolog" dbtype="query">
select * from qryCdCatalog order by id
</cfquery>
<table>
<th>ID</th> <th>TITLE</th> <th>ARTIST</th> <th>PRICE</th>
<!--- loop through the query result --->
<cfoutput query="qry_getcdcatolog">
<tr>
<td>#id#</td>
<td>#title#</td>
<td>#artist#</td>
<td>#price#</td>
</tr>
</cfoutput>
</table>
</cfif>
Conclusion
The query variable qry_getcdcatolog can be used in the application to display the xml node values on the screen. The main thing to remember in this example is once the xml data is loaded using Dom object, cold fusion can receive the xml node values on to the collection object which we can loop though and split it using the node names.
This method of reading xml data using Microsoft Dom object is really useful if we have small xml files to read in our applications. I hope this example helps you in your development.
|
|
Author: S R |
|
Send any comments at: contact@lizratechnologies.com
|