ColdFusion Xml Nodes Have An XmlNodes Property
Posted August 10, 2009 at 9:47 AM
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:
| | | | | |
| | ![]() | | ||
| | | |
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
"the other day, I was reading through the ColdFusion LiveDocs"
... don't you mean "every day ..."
@Steve,
Ha ha ha, lately it definitely feels like that with all the CF9 stuff :)
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.
@Brian,
Yeah, it seems to give you access to every type of node. I'm trying to think of a good use case.
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
@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.
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.




