Solved

Coldfusion, how to output XML file to HTML

Posted on 2009-03-29
9
930 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now