Solved

XMLSearch() function -  parsing xml search feed

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
character entities and entity references in XML 17 98
JQuery tracking event. 3 39
XSLT Help 12 19
Adjust the position 3 7
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

707 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

17 Experts available now in Live!

Get 1:1 Help Now