ColdFusion XMLSearch Behavior / Gotcha
April 21st, 2008
Just thought I’d point out that there is some odd behavior in XMLSearch() results. See the example below:
<cfxml variable="test">
<test>
<node>
<nodes>
<nodeline>node1</nodeline>
</nodes>
<nodes>
<nodeline>node2</nodeline>
</nodes>
<nodes>
<nodeline>node3</nodeline>
</nodes>
</node>
</test>
</cfxml>
<cfset results = XMLSearch(test, "//nodes")/>
<cfset results1 = results[1]/>
<cfdump var="#results1#">
Now if you run this you will see a dump that shows the root node as nodes but, if you try to reference the results as: results1.nodes.nodeline you will get an error. You have to reference the array as the root result. So the path to the nodelines becomes result.nodeline.
Hope that saves someone out there some headaches. I think that XMLSearch needs some major modification but, that may break backward compatibility so I guess Adobe is kinda locked in to how it works currently. For example, XMLSearch has some powerful features built in via XPath. However the way that ColdFusion returns the results is a bit bizzare. Well maybe not bizzare but, definately too verbose. So if you ask ColdFusion to return just the text assigned to the nodelines in the above example, it will return a full xml doc instead of just an array of values. So you end up parsing the data again. It would be really nice to just get an array of results back instead of an array of xml docs back. But, I digress.
If you want to learn XPath, just check out the docs here
2 Responses to “ColdFusion XMLSearch Behavior / Gotcha”
Sorry, comments are closed for this article.
April 22nd, 2008 at 07:35 AM
I’m not sure that is odd behaviour, it is consistent with how XPath works any XML root element in CF in my experience (ie you do not reference it).
Regarding your text nodes problem… Ben Nadel tried the very same. http://www.bennadel.com/blog/909-Ask-Ben-Selecting-All-Text-Nodes-Of-An-XML-Document.htm
And http://zvon.org is a pretty good resource
April 22nd, 2008 at 08:43 AM
@dc, it being consistent with how XPath behaves is an oddity because any other XML doc in CF doesn’t behave this way.
That article you referenced is a great example of how CF returns not just the text (which is what you asked it for) but an array of xml properties… I guess that is fine but, in my opinion not very useful.