Solved

XMLSearch() function -  parsing xml search feed

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…

728 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