Solved

referencing xml attributes inside an array

Posted on 2010-11-17
9
370 Views
Last Modified: 2012-05-10
I am trying to parse a 3rd party generic xml search feed in coldfusion so that I create a html output of the feed on my website. I have got a small way through solving the problem.

I will take through where I am at now:

To do this I am referencing a url that is not on my website. Let's call it http://www.externalxmlfeedurl.com

The xml file on the external url is in the following format:
<Results xsi:noNamespaceSchemaLocation="asdasd">
-
<ResultSet id="searchResults" numResults="3">
-
<Listing rank="1" title="175 <b>Hotels</b> in <b>Your Area</b>" description="Book your <b>hotel</b></b> online. Save up to 75% on your reservation." siteHost="www.destinationurlone.com/Hotels">
-
<ClickUrl type="body">
http://someurl1
</ClickUrl>
</Listing>
-
<Listing rank="2" title="<b></b> <b>Hotels</b> - Book Now" description="Up to 70% off <b></b> <b>Hotels</b>. Compare and book online now." siteHost="www.destinationurltwo.com/Hotels">
-
<ClickUrl type="body">
http://someurl2
</ClickUrl>
</Listing>
-
<Listing rank="3" title="<b></b> <b>Hotels</b>" description="Up to 75% Off in <b></b> <b>Hotels</b>. Easy Booking, Instant Confirmation" siteHost="destinationurltwo/hotels">
-
<ClickUrl type="body">
http://someurl3
</ClickUrl>
</Listing>
+
</ResultSet>
</Results>

What I am trying to do is to convert this xml data to a html output on one of my coldfusion pages
I am trying to get the output to be in the following format

Title 1
Description  1
URL 1

Title 2
Description  2
URL 2


Title 3
Description  3
URL 3

right now I am here with my code:

<cfhttp url="http://www.externalxmlfeedurl.com">

<CFSET xmlExternal = "#cfhttp.filecontent#">
<cfset xmlDoc  = XMLParse(xmlExternal )>
<cfset listings = XMLSearch(xmlDoc, "/Results/ResultSet/Listing")>

<cfloop from="1" to="#ArrayLen(listings)#" index="i">
<li><cfoutput>#listings.xmlAttributes.description[1]#</cfoutput> </li>
</cfloop>


However I get an error "You have attempted to dereference a scalar variable of type class coldfusion.runtime.Array as a structure with members. "

can you please let me know how to reference the xml attributes inside the array so I can get the values of title, description and url
0
Comment
Question by:bradderick
[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
  • 5
  • 4
9 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 34161003
#Listings# is the array not "description".  So instead of

       #listings.xmlAttributes.description[1]#

.. use
       #listings[1].xmlAttributes.description#

But replace 1 with your loop index "i". Otherwise you'll just be outputting the first element over and over.




0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 34161024
Btw: If you are using CF8+ you can use an "array" loop instead of from/to

Array loop
<cfoutput>
<cfloop array="#listings#" index="currListing">
      #currListing.xmlAttributes.description#...
</cfloop>
</cfoutput>
0
 

Author Comment

by:bradderick
ID: 34161032
please bear in mind that I am an absolute beginner at this
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 52

Expert Comment

by:_agx_
ID: 34161310
Once you make the changes I mentioned, what you have now will work fine.  My last comment just meant there's a simpler way to loop through arrays.  But it only works in CF8 or later.  If you're using CF8+ use example 1.  If you're  using an earlier version, then use example 2.

Unfortunately, I've got to head out now.  I don't know how familiar you are with arrays .. ? But if you've got more questions about this, I'd be happy to answer them tomorrow if another expert hasn't already.



Example 1 (Requires CF8+)
<cfoutput>
<cfloop array="#listings#" index="currListing">
     <li>#currListing.xmlAttributes.description#</li>
</cfloop>
</cfoutput>

Example 2
<cfoutput>
<cfloop from="1" to="#ArrayLen(listings)#" index="i">
      <li>#listings[i].xmlAttributes.description#</li>
</cfloop>
</cfoutput>

Open in new window

0
 

Author Comment

by:bradderick
ID: 34161855
Thanks Agx. I am not using CF8, I figured out that was probably the reason my code kept on breaking  when I used cffloop array so thanks for the extra example.

Can you please also tell me how to output the clickurl underneath the description inside the cfloop. Remembering that the 'clickurl' is an xml child of' listing'
0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 500 total points
ID: 34165835
Ah, ok. Sorry to confuse things.  I tend to assume CF8+ unless otherwise specified.

>> 'clickurl' is an xml child of' listing'

Use dot notation to access child nodes. The syntax is

         
#parentNode.childNode#

Open in new window

   
So this returns the whole "ClickUrl" node

      code:      #listings[i].clickURL#
      results:  <ClickUrl type="body">http://someurl2</ClickUrl>

Open in new window


To return only the node's text (ie "http://someurl2"), add the "xmlText" attribute

     code: #listings[i].clickURL.xmlText#
     results: http://someurl2

Open in new window


>> how to output the clickurl underneath the description inside the cfloop

... just add that line beneath your description:

<cfoutput>
<cfloop from="1" to="#ArrayLen(listings)#" index="i">
      <li>#listings[i].xmlAttributes.description#</li>
      <li>#listings[i].clickURL.xmlText#</li>
</cfloop>
</cfoutput>

Open in new window

0
 

Author Closing Comment

by:bradderick
ID: 34169101
Agx is a coldfusion genius
0
 

Author Comment

by:bradderick
ID: 34169108
Thanks Agx. Much appreciated.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 34169760
You're welcome :)
0

Featured Post

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.

Question has a verified solution.

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

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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