Solved

Coldfusion, how to output XML file to HTML

Posted on 2009-03-29
9
939 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
trying to resize an Image, using CFimage tag. 8 56
How can i point a subdomain to directory? 5 38
Combining Queries 7 38
Customizing XML with Powershell 2 31
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

735 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