Solved

referencing xml attributes inside an array

Posted on 2010-11-17
9
367 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Suggested Solutions

Title # Comments Views Activity
https json cfhttp post 1 148
cfspreadsheet 15 109
Database set up 5 79
trying to resize an Image, using CFimage tag. 8 42
The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

837 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