ColdFusion Xml Nodes Have An XmlNodes Property

Posted August 10, 2009 at 9:47 AM by Ben Nadel

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

Aug 10, 2009 at 10:01 AM // reply »
66 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 »
11,246 Comments

@Steve,

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


Aug 10, 2009 at 10:18 AM // reply »
26 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 »
11,246 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 »
67 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 »
11,246 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 »
67 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 A Comment

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.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 25, 2013 at 10:01 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Avi, Really glad to help! @Jaredwilli, I'm finding a this image hits home with a lot of people :) Hopefully we can all work through the rough patches together! @Prateek, AngularJS has error ... read »
May 25, 2013 at 9:53 PM
Nested Views, Routing, And Deep Linking With AngularJS
@Mrsean2k, I'm glad I could help! I haven't been able to keep up with the ui-router stuff. I keep saying that I'll carve out time, but I just haven't gotten to it :( ... read »
May 25, 2013 at 9:49 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, Thanks for the book recommendations. I am looking them up right now. I can see that Object Thinking is available for the Kindle App - sweet! Also, I just recently heard Martin Fowler on the ... read »
May 25, 2013 at 9:41 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
@Chris, I'm super excited to hear that my posts are helpful. I am also loving AngularJS; but, it definitely has some caveats and some odd behaviors and some things that just don't seem to "wor ... read »
May 25, 2013 at 9:36 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam, @Jason, After reading these comments, I double-checked my latest implementation and I am happy to report that I am using listFirst() and listRest(). ... read »
May 25, 2013 at 9:31 PM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Daxesh, I am not sure I understand the question about the current node. If you already have a reference to the current node, why would you need to query for it? As for parent node, I believe that ... read »
May 25, 2013 at 10:08 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Ben, my question is that i want the current node with its tag and its parent node. i just want only that data. So, give me the solution for that. and remember solution is working on " xpath 1.0 ... read »
May 25, 2013 at 10:01 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
hey ben, i want get my current node tag and also want the root node tag withing. So, how can i fix it.. ! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools