Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at Scotch On The Rocks (SOTR) 2011 (Edinburgh) with: Andy Weber and Gunnar Lieb

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

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.

Reply to this Comment

@Brian,

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

Reply to this Comment

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

Reply to this Comment

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

Reply to this Comment

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.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.