ColdFusion Xml Nodes Have An XmlNodes Property

Posted August 10, 2009 at 9:47 AM

Tags: ColdFusion

This is a really minor post, but the other day, I was reading through the ColdFusion LiveDocs for XML when I saw that XML nodes in ColdFusion XML documents have a property called, "XmlNodes." I wasn't sure how this would be different from the XmlChildren property so I built a quick demo page to what XmlNodes gives us:

 Launch code in new window » Download code as text file »

  • <!--- Build a simple ColdFusion XML document. --->
  • <cfxml variable="girlsXml">
  •  
  • <girls>
  • <!-- These ladies are awesome! -->
  • <girl>
  • <name>Carolyn</name>
  • <hair>Brunette</hair>
  • </girl>
  • <girl>
  • <name>Joanna</name>
  • <hair>Brunette</hair>
  • </girl>
  • </girls>
  •  
  • </cfxml>
  •  
  • <!---
  • Dump out the XML Nodes contained within the Girls root
  • node. This will give all the child nodes, not just the
  • element nodes.
  • --->
  • <cfdump
  • var="#girlsXml.xmlRoot.xmlNodes#"
  • label="xmlRoot.XmlNodes"
  • />

When we run this code we get the following CFDump output:

 
 
 
 
 
 
XmlNodes Property Provides A Collection Of All ChildNodes In A Given Xml Node, Including Comment And Text Nodes. 
 
 
 

As you can see from the output, the XmlNodes property gives us all child elements of a given node, regardless of the node type. This is different from the XmlChildren property which only returns Element node types. I am not sure what I would use this for exactly. I suppose if you needed to get at non-Element nodes, but maintain a sense of order this would be useful. I'll see if I can come up with an interesting use-case.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Aug 10, 2009 at 10:01 AM // reply »
55 Comments

"the other day, I was reading through the ColdFusion LiveDocs"

... don't you mean "every day ..."


Aug 10, 2009 at 10:04 AM // reply »
6,516 Comments

@Steve,

Ha ha ha, lately it definitely feels like that with all the CF9 stuff :)


Aug 10, 2009 at 10:18 AM // reply »
11 Comments

Interesting that it also picked up the comment line within the XML. Guess it doesn't discern the comment syntax as being special/not meant to be data.


Aug 10, 2009 at 10:21 AM // reply »
6,516 Comments

@Brian,

Yeah, it seems to give you access to every type of node. I'm trying to think of a good use case.


Aug 10, 2009 at 11:59 AM // reply »
64 Comments

I'd say the most obvious use case would be is if you needed to parse a doc that wasn't simply a series of nodes, but also had text nodes in it, eg:

<sentence>This is a sentence. It has <bold>bold</bold> and it has <italics>italics</italics> in it</sentence>

Obviously one would usually try to process this sort of thing with an XSLT, but if one needs to do it "by hand" then it's important to get all the content out - in order - not just the nested nodes.

--
Adam


Aug 10, 2009 at 12:25 PM // reply »
6,516 Comments

@Adam,

Yeah, I was thinking about something like that, I just can't quite come up with a good demo to explore. I agree that XSLT would probably be the easier way.


Aug 10, 2009 at 4:15 PM // reply »
35 Comments

Also, there are XML Parsers/Serializers that may reorganize text nodes into attributes, or vice-versa:

<person name="Rick Osborne"/>

vs

<person><name>Rick Osborne</name></person>

In either of those cases, you could still access person.xmlNodes.name and person.xmlNodes.name.

I know that the Perl module XML::Simple can do this via the appropriate config switches.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »