ColdFusion Xml Nodes Have An XmlNodes Property
Posted August 10, 2009 at 9:47 AM by Ben Nadel
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">
- <!-- These ladies are awesome! -->
- Dump out the XML Nodes contained within the Girls root
- node. This will give all the child nodes, not just the
- element nodes.
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.
- Wanted: Full-Time ColdFusion Developer at Intoria Internet Architects
- Cold Fusion Senior Developer at Edge Information Management
- Back-End Web Developer-Information Technologist at Michigan State University
- ColdFusion Developer at Nonfat Media
- Mid-to-Senior Level Web Application Developer at SiteVision, Inc.
"the other day, I was reading through the ColdFusion LiveDocs"
... don't you mean "every day ..."
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.
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.
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"/>
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.