Solved

referencing xml attributes inside an array

Posted on 2010-11-17
9
368 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
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

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.

Question has a verified solution.

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

Suggested Solutions

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
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 …

756 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