Solved

Coldfusion, how to output XML file to HTML

Posted on 2009-03-29
9
933 Views
Last Modified: 2013-12-24
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

0
Comment
Question by:CurtinProp
  • 4
  • 3
  • 2
9 Comments
 
LVL 2

Author Comment

by:CurtinProp
ID: 24015992
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
 
LVL 2

Accepted Solution

by:
albrandwood earned 250 total points
ID: 24016015
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
 
LVL 2

Expert Comment

by:albrandwood
ID: 24016023
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 27

Assisted Solution

by:azadisaryev
azadisaryev earned 250 total points
ID: 24016063
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
 
LVL 27

Expert Comment

by:azadisaryev
ID: 24016091
bugger my terribly slow typing! while i was typing my one comment, @shley managed to post 2!

Azadi
0
 
LVL 2

Author Comment

by:CurtinProp
ID: 24016555
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
 
LVL 27

Expert Comment

by:azadisaryev
ID: 24016585
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
 
LVL 27

Expert Comment

by:azadisaryev
ID: 24016677
here, found this one for you:
http://www.experts-exchange.com/help.jsp?hi=407

Azadi
0
 
LVL 2

Author Comment

by:CurtinProp
ID: 24016698
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

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question