Getting The String Representation Of One ColdFusion XML Node

Posted September 20, 2007 at 3:27 PM by Ben Nadel

Tags: ColdFusion

I can't remember how this came up, but the other day I was talking to this girl about XML; it was a pretty engaging conversation, as you can probably well imagine. I was trying to explain the difference between a ColdFusion XML document object and an XML string when the question occurred to me - can you use ToString() to get a partial representation of an XML document object.

Traditionally, you can call ToString() on an entire ColdFusion XML document to get an XML string representation:

  • #ToString( xmlDocumentObject )#

But, what about calling that on an individual XML node that is within the XML document. I gave it a go:

  • <!--- Define our ColdFusion XML document object. --->
  • <cfxml variable="xmlGirls">
  •  
  • <girls>
  • <girl>
  • <name>Samantha</name>
  • <age>27</age>
  • <hair>Blonde</hair>
  • </girl>
  • <girl>
  • <name>Kim</name>
  • <age>32</age>
  • <hair>Brunette</hair>
  • </girl>
  • <girl>
  • <name>Cindi</name>
  • <age>25</age>
  • <hair>Black</hair>
  • </girl>
  • </girls>
  •  
  • </cfxml>
  •  
  •  
  • <!--- Get the third girl. --->
  • <cfset arrNodes = XmlSearch( xmlGirls, "//girl[ 3 ]" ) />
  •  
  • <!--- Get a pointer to the girl node. --->
  • <cfset xmlGirl = arrNodes[ 1 ] />
  •  
  • <!---
  • Output the string representation of JUST this node
  • taken from the XML document.
  • --->
  • #HtmlEditFormat(
  • ToString( xmlGirl )
  • )#

As you can see, we are calling ColdFusion's ToString() method on only the third girl element node in the XML document. Running this gives us the following output:

  • <?xml version="1.0" encoding="UTF-8"?>
  • <girl>
  • <name>Cindi</name>
  • <age>25</age>
  • <hair>Black</hair>
  • </girl>

Pretty cool! Not only did it give us the string representation of the single XML node, it also gave us the XML document type.

NOTE: I modified the tabbing of the outputted string for easier reading.




Reader Comments

Sep 20, 2007 at 4:23 PM // reply »
79 Comments

Definitely did not know you could do that. Cool, and thanks for pointing it out!


Sep 20, 2007 at 4:41 PM // reply »
11,246 Comments

No worries. Been doing a lot of XML learning lately.


Apr 21, 2008 at 3:09 AM // reply »
2 Comments

Great tips, thanks for it.


Oct 15, 2008 at 4:34 PM // reply »
3 Comments

Hi Ben, do you know of an easy way to strip the XML declaration or to get the string representation without the XML declaration?


Oct 15, 2008 at 4:50 PM // reply »
3 Comments

I did this:

<cfset xmlString = ToString(xmlNode)>
<cfif xmlString contains "<?xml">
<cfset xmlString=ListDeleteAt(xmlString ,1,">")>
<cfset xmlString=Right(xmlString ,Len(xmlString )-1)>
</cfif>

It's ugly, but it seems to do the trick.


Oct 15, 2008 at 5:24 PM // reply »
11,246 Comments

@Nat,

You can use a regular expression (regEx) to remove the declaration:

XmlString = REReplace( XmlString, "<\?xml[^>]*>", "", "one" )


Oct 15, 2008 at 7:18 PM // reply »
3 Comments

That's a good solution. Thanks.


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