Solved

XMLSearch() function -  parsing xml search feed

Posted on 2010-11-16
3
559 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

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 …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

749 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