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  |  Print Page





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 »
7,572 Comments

@Steve,

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


Aug 10, 2009 at 10:18 AM // reply »
14 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 »
7,572 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 »
7,572 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 »
44 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
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »
Mar 19, 2010 at 4:47 PM
Application.cfc OnRequest() Method Affects OnError() Arguments
@Jason and @Ben, I've been doing some CF9 refactoring on our systems and noticed an odd occurrence with onError as well. Found a way to work around my problem, but what I saw was... Background: Our ... read »
Jim
Mar 19, 2010 at 4:44 PM
Shoot 'Em Up Starring Clive Owen And Paul Giamatti
I actually enjoyed this movie quite a lot. It was different, certainly, but I think they were going for more of a Quentin Tarentino-"wow, that was weird"-vibe than an actual spoof. Once I realize ... read »
Mar 19, 2010 at 4:34 PM
An Intensive Exploration Of jQuery With Ben Nadel (Video Presentation)
Hey I guess the video is down. Is there anyway you can upload to youtube or vimeo or some other service? Greatly appreciated. ... read »
Mar 19, 2010 at 4:24 PM
ColdFusion CFPOP - My First Look
@Ben Thanks for the follow up! The root of the problem had to do with being able to trace bounced emails to specific records in a DB table. Let's say you run an email campaign and you get 1,000 bou ... read »