Getting The String Representation Of One ColdFusion XML Node

Posted September 20, 2007 at 3:27 PM

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:

 Launch code in new window » Download code as text file »

  • #ToString( xmlDocumentObject )#

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

 Launch code in new window » Download code as text file »

  • <!--- 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:

 Launch code in new window » Download code as text file »

  • <?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.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

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

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


Sep 20, 2007 at 4:41 PM // reply »
7,572 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 »
7,572 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 Comment  |  Ask Ben

Recent Blog Comments
Mar 22, 2010 at 7:43 AM
Terms Of Service / Privacy Policy Document Generator
Thankyou for this very helpful form. You've made my life much easier today. I'll have a look around your site... I'm sure there's some more good stuff here..Thanks Dave ... read »
Mar 22, 2010 at 7:21 AM
Encountered "(. Incorrect Select Statement, Expecting a 'FROM', But Encountered '(' Instead, A Select Statement Should Have a 'FROM' Construct.
I got this exception now. In case you're using var-es local struct, CF gives you couple of "new" exceptions: Encountered "local. and Encountered "id. Incorrect Select List, Incorrect select colum ... read »
Mar 22, 2010 at 3:08 AM
Ask Ben: Selecting XML Attributes Given Other XML Attributes
Thanks for the response. I finally discovered that I was getting this error because I had cfsetting enablecfoutputonly="yes" in Application.cfc, and was neither setting it to false elsewhere nor brac ... read »
Mar 21, 2010 at 8:57 PM
The Bourne Ultimatum Starring Matt Damon And Julia Stiles
late to the party, but my observation is this: rewatch carefully for the platonic nature of the relationship between nicki and jason. she never flirts with him. he never comes on to her. they alway ... read »
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »