Coldfusion, how to output XML file to HTML

I Have an xml file and I'd like to output certain data from it as I would a normal query in coldfusion.

The structure of the XML file is as follows:

XMLfile name: hotspots.xml

xml structure: (please see code snippet for xml structure)

I'd just like to create a page in coldfusion that outputs the following data from the xml file:

NAME,
X,
Y,

can anyone walk me through outputting xml into a coldfusion page?
xml structure:
 
<HOTSPOT 
		ID="1"
		NAME="name in here"
		MEDIATYPE="url"
		MEDIA="hotspots/pointer_left.png"
		X="2681"
		Y="6392"
		ZOOM="70" 
		XSCALE=""
		YSCALE=""
		URL=""
		URLTARGET="_blank"
		ROLLOVER="0"
		CAPTION=""
		TOOLTIP="" >
    <CAPTION> </CAPTION>
    <TOOLTIP></TOOLTIP>
  </HOTSPOT>

Open in new window

LVL 2
CurtinPropAsked:
Who is Participating?
 
albrandwoodConnect With a Mentor Commented:
There are two ways of doing it ... The "correct" method assumes the XML sheet is defined 100% correctly

assuming xml_string is the string holding the XML sheet.

If the XML sheet is valid (and that's by Adoe's definition, not yours)
then use XMLParse to convert the XML sheet into a structure (use cfdump to view the structure) ... Your data will appear below an array of hotspots somewhere in the tree, in a sub-structure called "XMLAttributes" ... referenced by XMLRoot.Hotspot[i].XMLAttributes["attrib"] (Note that it may be case sensitive, I don't recall)

if the XML sheet is not valid, then you have to parse the sheet manually ... not as efficient, but works when the XML sheet is badly formated.

(Note that there may be minor gramatical or typographical errors in this code example as I am writing this from memory)

@shley
<!--- Answers to be stored in an array --->
<cfset hotspots=ArrayNew(1)>
<cfif(isXML(xml_string))>
    <!--- XML string is a Valid XML --->
    <!--- Parse XML into a Struct --->
    <cfset xml_struct=XMLParse(xml_string)>
    <!--- use cfdump to understand the xml_struct and modify the loop below to suit --->
    <cfloop from="1" to="#ArrayLen(XMLRoot.Hotspot)#" index="i">
        <cfset hotspot=StructNew()>
        <cfloop list="Name,X,Y" index="attrib">
            <cfif IStructKeyExists(XMLRoot.Hotspot[i].XMLAttributes,attrib)>
                <cfset StructInsert(hotspot,attrib,XMLRoot.Hotspot[i].XMLAttributes[attrib],true)>
            <cfelse>
                <cfset StructInsert(hotspot,attrib,iif(attrib eq "Name",'""','"0"'),true)>
            </cfif>
        </cfloop>
        <cfset ArrayAppend(hotspots,hotspot)>
    </cfloop>
<cfelse>
    <!--- find the 1st case of <hotspot[^>]*> --->
    <cfset hotspot_find=ReFindNoCase(xml_string,"<hotspot[^>]*>",1,true)>
    <!--- Loop while hotspot_find is successful --->
    <cfloop condition="#hotspot_find.len[1]#">
        <!--- create a structure to hold the answers --->
        <cfset hotspot=StructNew()>
        <--- extract the individual <hotspot> element --->
        <cfset hotspot_data=mid(xml_string,hotspot_find.pos[1],hotspot_find.len[1])>
        <!--- loop for the required attributes --->
        <cfloop list="name,x,y" index="attrib">
            <!--- search for the attributes --->
            <cfset attrib_find=ReFindNoCase(hotspot_data,'#Attrib#="[^"]*"',1,true)>
            <cfif ArrayLen(attrib_find.len) eq 2 and attrib_find.len[1]>
                <!--- Attribute exists and has value --->
                <cfset structInsert(hotspot,attrib,mid(hotspot_data,attrib_find.pos[1],attrib_find.len[1]),true)>
            <cfelse>
                <!--- Attribute doesn't exist or has no value --->
                <cfset structInsert(hotspot,attrib,iif(attrib eq "name",'""','"0"'),true>
            </cfif>
        </cfloop>
        <!--- Append the struct hotspot to the array hotspots --->
        <cfset ArrayAppend(hotspots,hotspot)>
        <!--- Locate the next <hotspot> element --->
        <cfset hotspot_find=ReFindNoCase(xml_string,"<hotspot[^>]*>",hotspot_find.pos[1]+hotspot_find.len[1],true)>
    </cfloop>
</cfif>
<cfdump var="#hotspots#">

Open in new window

0
 
CurtinPropAuthor Commented:
I've gotten to a stage now where I can output data, however it only outputs the data for one item, not all the items in the XML file.

my code so far is below:
<CFFile action="READ" variable="xml" file="#ExpandPath('.')#/hotspots.xml">
 
<CFSet xmlDoc = XMLParse(xml)>
 
 
 
<!--- dump for whole file
 
<CFDump var="#xmlDoc#">--->
 
 
<!--- The output below only outputs one item, how do I output all attributes of "NAME" ? --->
 
<cfoutput>
 
#xmlDoc.XmlRoot.hotspot.XmlAttributes.NAME#
 
</cfoutput>

Open in new window

0
 
albrandwoodCommented:
treat xmlDoc.XmlRoot.hotspot as an array ...
 



<cfoutput>
<cfloop from="1" to="#ArrayLen(xmlDoc.XmlRoot.hotspot)#" index="i">
    Name=#xmlDoc.XmlRoot.hotspot[i].XmlAttributes.NAME#
    X=#xmlDoc.XmlRoot.hotspot[i].XmlAttributes.X#
    Y=#xmlDoc.XmlRoot.hotspot[i].XmlAttributes.Y#<br>
</cfloop>
</cfoutput>

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
azadisaryevConnect With a Mentor Commented:
assuming your xml looks like this:
<?xml version="1.0" encoding="UTF-8" ?>
<HOTSPOTS ...>
    <HOTSPOT
                ID="1"
                NAME="name in here"
                MEDIATYPE="url"
                MEDIA="hotspots/pointer_left.png"
                X="2681"
                Y="6392"
                ZOOM="70"
                XSCALE=""
                YSCALE=""
                URL=""
                URLTARGET="_blank"
                ROLLOVER="0"
                CAPTION=""
                TOOLTIP="" >
        <CAPTION> </CAPTION>
        <TOOLTIP></TOOLTIP>
    </HOTSPOT>
...(more HOTSPOT nodes) ...
</HOTSPOTS>


1) use XMLParse() function to parse your xml file into an xml object:
<cfset myxml = xmparse("path\to\hotspots.xml")>

2) create an array of all HOTSPOT nodes:
<cfset arrHotspots = myxml['HOTSPOTS']['HOTSPOT']>

4) loop over arrHotspots and output required xml attributes:
<cfoutput>
<cfloop from="1" to="#arraylen(arrHotspots)#" index="i">
NAME: #arrHotspots[i].xmlattributes['NAME']#<br />
X: #arrHotspots[i].xmlattributes['X']#<br />
Y: #arrHotspots[i].xmlattributes['Y']#<br /><br />
</cfloop>
</cfoutput>

if you are on cf8, then the loop can be coded as:
<cfloop array="#arrHotspots#" index="hs">
NAME: #hs.xmlattributes['NAME']#<br />
X: #hs.xmlattributes['X']#<br />
Y: #hs.xmlattributes['Y']#<br /><br />
</cfloop>

PS: all the above is very much from memory, so sorry if you get some syntax errors... post here if the above does not work or if you get any errors or if you are not clear about something.

Azadi
0
 
azadisaryevCommented:
bugger my terribly slow typing! while i was typing my one comment, @shley managed to post 2!

Azadi
0
 
CurtinPropAuthor Commented:
Hi guys, thanks for the detailed replies.... both are pretty much spot on in their own rights (both work barring a couple of changes) so how do I allocate the points? 50/50???
0
 
azadisaryevCommented:
i really have no idea! never asked questions here - only answered :)
maybe check the EE guide/support pages - they should have it explained somewhere surely...

Azadi
0
 
azadisaryevCommented:
here, found this one for you:
http://www.experts-exchange.com/help.jsp?hi=407

Azadi
0
 
CurtinPropAuthor Commented:
I've decided to plit the points 250 each, This is basically because both answers gave me a pretty comprehensive step by step solution and they both worked without too much modification, and they were submitted really close together!

 I hope this is fine with the two of you, I really appreciate it when experts get back to me quickly, thanks again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.