Solved

Coldfusion, how to output XML file to HTML

Posted on 2009-03-29
9
941 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Simple, centralized multimedia control

Watch and learn to see how ATEN provided an easy and effective way for three jointly-owned pubs to control the 60 televisions located across their three venues utilizing the ATEN Control System, Modular Matrix Switch and HDBaseT extenders.

 
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

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

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

Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
What You Need to Know when Searching for a Webhost Provider
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

739 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