Solved

referencing xml attributes inside an array

Posted on 2010-11-17
9
363 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_
Comment Utility
#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
Comment Utility
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
Comment Utility
please bear in mind that I am an absolute beginner at this
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:bradderick
Comment Utility
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
Comment Utility
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
Comment Utility
Agx is a coldfusion genius
0
 

Author Comment

by:bradderick
Comment Utility
Thanks Agx. Much appreciated.
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
You're welcome :)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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