Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
Ben Nadel at the New York ColdFusion User Group (Feb. 2009) with: Jack Welde
Ben Nadel at the New York ColdFusion User Group (Feb. 2009) with: Jack Welde@jwelde )

ColdFusion Xml Nodes Have An XmlNodes Property

By Ben Nadel on
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:

  • <!--- 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.

Reader 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.



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.

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

<person name="Rick Osborne"/>


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

In either of those cases, you could still access and

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