Solved

XMLSearch() function -  parsing xml search feed

Posted on 2010-11-16
3
552 Views
Last Modified: 2012-08-13
Hi There,

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.

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

Ad Title 1
Ad Description  1
Ad URL 1

Ad Title 2
Ad Description  2
Ad URL 2


Ad Title 3
Ad Description  3
Ad URL 3

I am very inexperienced using xml and the XmlSearch() and XMLParse() functions. Can you please assist me understand what I need to do to get this done.
0
Comment
Question by:bradderick
  • 3
3 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 34156619
First use XMLParse() to convert the string into an xml doc.  Then you can use xml functions on it.
       <cfset xmlDoc  = XMLParse( theStringContent )>

Then you can use extract the listings with a simple XPath expression.  The "Listings" node is a child of "ResultSet" which is a child of the root node "Results"

<Results>
---<ResultSet>
-------<Listing>

... so the path to that node is:    

/Results/ResultSet/Listing

The syntax is a bit like url's where "/" is the web root.  But with XPath they're case sensitive.  Using this path with XMLSearch will return an array of "Listing" nodes, or any empty array if the path wasn't found.  Just through the array to display the results.


<cfset listings = XMLSearch(xmlDoc, "/Results/ResultSet/Listing")>
<cfloop array="#listings#" index="currListing"> ....</cfloop>

Just like with cfml tags, "Title" and "Description" are attributes of the Listing node.  So you'll use the XMLAttributes property to access those values:

      #currListing.xmlAttributes.Description#

The "clickURL" is a child of the Listing node


<Listing>
---<ClickURL>text</ClickURL>

... and like a structure, can be accessed with array notation  ie #structName.keyName#.  But unlike "Title" and "Description" the url value is not an attribute.  It is the text of the node. So you must use the XMLText property to access that value

         
#currListing.clickURL.XMLText#


0
 
LVL 52

Expert Comment

by:_agx_
ID: 34156650
>> Just like with cfml tags, "Title" and "Description" are attributes of the Listing node

ie  Rank, Title and Description are all attributes

         <Listing rank="1" title="Something ....." Description="Blah,blah ....">
         </Listing>
0
 
LVL 52

Expert Comment

by:_agx_
ID: 34170020
>> and like a structure, can be accessed with array notation  ie #structName.keyName#

Correction for the archives:
       That should be dot notation, instead of "array" notation :)
0

Featured Post

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.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

867 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

24 Experts available now in Live!

Get 1:1 Help Now