Hopefully, the error of my ways will be obvious to thos
of you who work with xml a lot, but...HELP!!!
Given this demo xml:
<?xml version="1.0"?>
<Products>
<Category ID="1">Air Conditioners</Category>
<SubCategory ID="1">
<SubCategoryName>Air Conditioners</SubCategoryName>
<ShortDescription/>
<LongDescription/>
<Product ID="XC21" New="N" EnergyStar="Y" SortOrder="100">
<ModelNumber>XC21</ModelNumber>
<ModelName>XC21 Air Conditioner</ModelName>
<OneLiner>The most quiet and efficient central air
conditioner you can buy</OneLiner>
<Ratings>
<Rating Type="SEER">up to 20.5</Rating>
<Rating Type="Sound">69</Rating>
</Ratings>
<SmallImage>http://www.lennox.com/res/photos/44383_small.jpg</SmallImage>
<PriceGuide>$$$</PriceGuide>
</Product>
(product repeats ...)
What's wrong with this code?
I constantly get the error:
"The value coldfusion.xml.XmlNodeList cannot be converted to a number."
The error occurred on line 44, which reads:
<cfset arrResult = xmlSearch(xmllennox-products,xp)>
Suggests, good tutorials? The one I've been following
isn't quite complete enough the complexity of my xml sheet.
Rick
============================================================================
====
<html>
<head>
<title>Lennox XML Search Test</title>
</head>
<body>
<h1>xmlSearch()</h1>
<cfset xmlLennox = xmlParse(expandPath('lennox-products.xml'))>
<cfoutput>
<table border='1' cellpadding='2' cellspacing='0'>
<tr>
<th>XPath</th>
<th>Result</th>
</tr>
<cfset arrXPs = arrayNew(1)>
<cfset arrayAppend(arrXPs,'/products')>
<cfset arrayAppend(arrXPs,'/products/category')>
<cfset
arrayAppend(arrXPs,'/products/category/subCategory')>
<cfset
arrayAppend(arrXPs,'/products/category/subCategory/subCategoryName')>
<cfset
arrayAppend(arrXPs,'/products/category/subCategory/shortDescription')>
<cfset
arrayAppend(arrXPs,'/products/category/subCategory/longDescription')>
<cfset
arrayAppend(arrXPs,'/products/category/subCategory/product/')>
<cfset
arrayAppend(arrXPs,'/products/category/subCategory/product/modelNumber')>
<cfset
arrayAppend(arrXPs,'/produtts/category/subCategory/product/modelName')>
<cfset
arrayAppend(arrXPs,'/products/category/subCategory/product/modelName/text()'
)>
<cfset
arrayAppend(arrXPs,'/products/category/subCategory/product/oneLiner')>
<cfset
arrayAppend(arrXPs,'/products/category/subCategory/product/ratings')>
<cfset
arrayAppend(arrXPs,'/products/category/subCategory/product/smallImage')>
<cfset
arrayAppend(arrXPs,'/products/category/subCategory/product/priceGuide')>
<cfloop index='index' from='1'
to='#arrayLen(arrXPs)#'>
<cfset xp = arrXPs[index]>
<cfset arrResult =
xmlSearch(xmllennox-products,xp)>
<tr valign='top'>
<td>#xp#</td>
<td>
<ul>
<cfloop
index='index' from='1' to='#arrayLen(arrResult)#'>
<li>#xmlGetNodeType(arrResult[index])#:
<cftry>
#arrResult[index].xmlText#
<cfcatch type='any'>#arrResult[index]#</cfcatch>
</cftry>
</li>
</cfloop>
</ul>
</td>
</tr>
</cfloop>
</table>
</cfoutput>
</body>
</html>
-----Original Message-----
From: James Holmes [mailto:[email protected]]
Sent: Friday, December 30, 2011 8:14 PM
To: cf-talk
Subject: Re: XML! Aaargh! How do I do this?
If you are just after products, you can dramatically simplify the code by
using xmlsearch() to get them with xpath instead of manually looping
multiple levels down.
--
Shu Ha Ri: Agile and .NET blog
http://www.bifrost.com.au/
On 31 December 2011 08:50, Justin Scott <[email protected]> wrote:
>
> > Okay... I've worked this every way I can think
> > of and, I admit it, I just don't get it.
>
> Hi Rick, replace your inner-most loop with the following. It should
> get you going in the right direction. It looks like there is another
> layer of sub-categories which needs to be accounted for:
>
> <!--- Loop over the sub-categories --->
> <cfloop from='1' to='#arrayLen(xmlProductContent.products.SubCategory)#'
> index='subCategoryCount'>
>
> <!--- Localize our current sub-category --->
> <cfset thisSubCategory =
> xmlProductContent.products.SubCategory[subCategoryCount] />
> <h2>#thisSubCategory.SubCategoryName.xmlText#</h2>
>
> <!--- Loop over the products in this sub-category --->
> <cfloop from="1" to="#arrayLen(thisSubCategory.Product)#"
> index="productCount">
> <!--- Localize our current product for easier access --->
> <cfset thisProduct = thisSubCategory.Product[productCount] />
> <!---<cfdump var="#thisProduct#" />--->
> <p>
> ID: #thisProduct.xmlAttributes.ID#<br />
> Model Name: #thisProduct.ModelName.xmlText#<br />
> Image: <img src="#thisProduct.SmallImage.xmlText#" />
> </p>
> </cfloop> <!--- Products --->
>
> </cfloop> <!--- Sub-Categories --->
>
>
> -Justin Scott
>
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive:
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:349284
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm