Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at InVision Office 2011 (New York City) with: Lindsey Root and Adam Root

Getting The String Representation Of One ColdFusion XML Node

By Ben Nadel on
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

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

Reply to this Comment

I did this:

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

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

Reply to this Comment


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

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

Reply to this Comment

Instead of

<cfset arrNodes = XmlSearch( xmlGirls, "//girl[ 3 ]" ) />
<!--- Get a pointer to the girl node. --->
<cfset xmlGirl = arrNodes[ 1 ] />


<cfset xmlGirl = XmlSearch( xmlGirls, "//girl[ 3 ]" ) [1]/>

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
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.