Solved

referencing xml attributes inside an array

Posted on 2010-11-17
9
366 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
need some regex help 15 29
Coldfusion update database with CFLOOP 5 64
Detect and combat possible robot 7 107
Restore of mysql database from .SQL file - using Coldfusion 5 34
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 …
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

9 Experts available now in Live!

Get 1:1 Help Now