ColdFusion and the OpenSRS name_suggest API (Part III)

OpenSRS API Add comments

Now we have to parse the results.  In my example, I wanted results for the straight lookup, the premium names and the suggested names, so I take the XML and use it to make three queries.  That part looks like this:

<!--- LOOKUP --->

<cfset searchFor = XMLSearch(theResult, "//data_block/dt_assoc//dt_assoc//item[@key='lookup']//dt_assoc//item[@key='items']//dt_array/item/dt_assoc/")>

<cfset countLookups = ArrayLen(searchFor)> 
 
<!--- Let's create a query to hold the actual lookup data --->

<cfset qLookup = QueryNew("domainName, result")>

<cfif #countLookups# GTE 1>

<cfset temp = QueryAddRow(qLookup, countLookups)>

<cfloop from="1" to="#countLookups#" index="thisRecord">

<cfset lookup1 = #searchFor[thisRecord].item[1].XmlText#>
<cfset lookup2 = #searchFor[thisRecord].item[2].XmlText#>

<cfset temp = QuerySetCell(qLookup, "domainName", lookup1, thisRecord)>
<cfset temp = QuerySetCell(qLookup, "result", lookup2, thisRecord)>

</cfloop>

</cfif>

<!--- PREMIUM --->

<cfset searchFor = XMLSearch(theResult, "//data_block/dt_assoc//dt_assoc//item[@key='premium']//dt_assoc//item[@key='items']//dt_array/item/dt_assoc/")>

<cfset countPremiums = ArrayLen(searchFor)> 

<!--- Let's create a query to hold the actual lookup data --->

<cfset qPremium = QueryNew("domainName, result, price")>

<cfif #countPremiums# GTE 1>

<cfset temp = QueryAddRow(qPremium, countPremiums)>

<cfloop from="1" to="#countPremiums#" index="thisRecord">

<cfset lookup1 = #searchFor[thisRecord].item[1].XmlText#>
<cfset lookup2 = #searchFor[thisRecord].item[2].XmlText#>
<cfset lookup3 = #searchFor[thisRecord].item[3].XmlText#>

<cfset temp = QuerySetCell(qPremium, "domainName", lookup1, thisRecord)>
<cfset temp = QuerySetCell(qPremium, "result", lookup2, thisRecord)>
<cfset temp = QuerySetCell(qPremium, "price", lookup3, thisRecord)>

</cfloop>

</cfif>

<!--- SUGGESTION --->

<cfset searchFor = XMLSearch(theResult, "//data_block/dt_assoc//dt_assoc//item[@key='suggestion']//dt_assoc//item[@key='items']//dt_array/item/dt_assoc/")>

<cfset countSuggestions = ArrayLen(searchFor)> 

<!--- Let's create a query to hold the actual lookup data --->

<cfset qSuggestion = QueryNew("domainName, result")>

<cfif #countSuggestions# GTE 1>

<cfset temp = QueryAddRow(qSuggestion, countSuggestions)>

<cfloop from="1" to="#countSuggestions#" index="thisRecord">

<cfset lookup1 = #searchFor[thisRecord].item[1].XmlText#>
<cfset lookup2 = #searchFor[thisRecord].item[2].XmlText#>

<cfset temp = QuerySetCell(qSuggestion, "domainName", lookup1, thisRecord)>
<cfset temp = QuerySetCell(qSuggestion, "result", lookup2, thisRecord)>

</cfloop>

</cfif>

The only thing really hairy about that is doing the XMLSearch for the XPATH.  That was a little odd for me the first go around, but the rest of it is pretty standard fare.

That’s it!  You started off with just a keyword, and now you have 3 queries you can display.   Here they are CFDUMPed.  The only thing left to do is make it pretty!  I’ll leave that up to you.



0 responses to “ColdFusion and the OpenSRS name_suggest API (Part III)”

Leave a Reply

Leave this field empty:

Powered by Mango Blog. Initial Template Design and Icons by N.Design Studio, modified by Michael Cummins.