?
Solved

XMLSearch() function -  parsing xml search feed

Posted on 2010-11-16
3
Medium Priority
?
567 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 2000 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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

765 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