Ask Ben: Create A New XML Document Based On A Child Node Of An Existing XML Document

Posted June 12, 2008 at 8:40 AM by Ben Nadel

Tags: ColdFusion, Ask Ben

Yesterday, someone asked me about creating new XML documents. They had an XML packet that came back from a web service call. This XML had some extra information in it that had to do with the web service call itself, not the data that was being transported. As such, they wanted to create a new XML document based on the target XML data, without the superfluous web service node data. For example, let's say that the web service returned something like this:

  • <envelope>
  • <body>
  • <actress>
  • <name>Maria Bello</name>
  • <birthday>April 18, 1967</birthday>
  • <url>http://www.imdb.com/name/nm0004742/</url>
  • </actress>
  • </body>
  • </envelope>

Here, the Envelope and Body node tags are protocols of the web service call and have nothing to do with the actual data that is being returned. As such, he wanted to take the Actress node and convert that to its own XML document in which Actress is the root node.

You're initial instinct might tell you just to try and copy the node using something like this:

  • <!--- Create new XML document. --->
  • <cfset xmlActress = XmlNew() />
  •  
  • <!--- Copy XML node to new document. --->
  • <cfset xmlActress.XmlRoot = xmlResponse.envelope.body.actress />

While this looks good, if you have messed around with XML a bunch, you will know that this is going to throw a document ownership error:

WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it. null.

XML nodes are owned by a given document and you can't simply copy a node from one XML document to another document (even if you try to Duplicate() the node first). At least, not with basic ColdFusion functions. If, however, you dig around in the Java methods, you will find that you can import nodes from one document to another.

But, there's actually an even simpler, ColdFusion-native way to do this - create a new ColdFusion XML document out of the given node. I know that sounds like I am answering the question with the question, but when you see the following, you will see that it is quite literally true:

  • <!---
  • We are going to assume this is some XML coming back from a
  • web service. We are conveting it to an actual ColdFusion XML
  • document object.
  • --->
  • <cfxml variable="xmlResponse">
  •  
  • <envelope>
  • <body>
  • <actress>
  • <name>Maria Bello</name>
  • <birthday>April 18, 1967</birthday>
  • <url>http://www.imdb.com/name/nm0004742/</url>
  • </actress>
  • </body>
  • </envelope>
  •  
  • </cfxml>
  •  
  •  
  • <!---
  • Convert the Actress node to a totally new XML document
  • of it's own. The trick here is that XmlParse() requires a
  • string, so ColdFusion will auto-convert the node into an
  • XML string of it's own. Then, XmlParse() takes that and
  • creates a totally new ColdFusion XML document object
  • from that.
  • --->
  • <cfset xmlActress = XmlParse(
  • xmlResponse.envelope.body.actress
  • ) />
  •  
  • <!--- Output new ColdFusion XML document. --->
  • <cfdump
  • var="#xmlActress#"
  • label="New Actress XML Document"
  • />

If you ignore the CFXml tag and the CFDump tag, the above is really just one line of code:

  • <cfset xmlActress = XmlParse(
  • xmlResponse.envelope.body.actress
  • ) />

As you can see, this is about as straight forward as I said it would be - you are creating a new ColdFusion XML document out of the existing node. That's what it looks like, but of course, that's not exactly what's happening. XmlParse() expects a string value and ColdFusion, in its infinite wisdom, will do whatever it can to make that happen. As such, it takes the Actress XML node from the original XML document and converts it into a string. The same effect would be accomplished with this call:

  • ToString( xmlResponse.envelope.body.actress )

... we are just letting ColdFusion do this implicitly. This creates an XML string in which the Actress node is the root node. Then, our XmlParse() call takes that and creates a totally new XML document based on that intermediary XML string. Pretty slick. And, so you can see this work, when we run the code above, here is the CFDump we get:


 
 
 

 
ColdFusion XML Document Based On Child Node Of Other XML Document  
 
 
 

Now, because we are conerting to and from string values, this is not going to be the fastest process, but it's one line of code, and that's the trade off.


 
 
 

 
This Post Has Been Approved By The Extremely Sexy Maria Bello  
 
 
 



Reader Comments

There are no comments posted for this web log entry.

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
Jun 18, 2013 at 3:39 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
Hi Ben, THANKS! While not bleeding edge, it is new to me & I like learning new things every day! ... read »
Jun 18, 2013 at 12:30 PM
Disabling Auto-Correct And Auto-Capitalize Features On iPhone Inputs
Also spellcheck="false" should be mentioned as part of html5 specs ... read »
Jun 18, 2013 at 8:40 AM
Using Named Functions Within Self-Executing Function Blocks In Javascript
Hi Ben, you forgot to mention the most important thing for named self-executing functions - they can be referenced by name ONLY inside their execution context (which is parens in this case), it mean ... read »
dee
Jun 18, 2013 at 7:01 AM
My Safari Browser SQLite Database Hello World Example
hai ben, this program is really good i could understand the concept but i dint know how to save it and how to open it as you have done in the video can u give that details pls ... read »
Jun 18, 2013 at 6:04 AM
Clearing Inline CSS Properties With jQuery
Thanks a lot for for post! It helped me a lot... after being stuck since 24 hrs.. found solution from your post. Thanks again! ... read »
Jun 18, 2013 at 2:31 AM
SOTR 2013 - The Best Conference I Never Went To
I keep watching it, should keep me happily distracted until SotR14 ;) ... read »
Jun 17, 2013 at 9:45 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, As I was reading what you wrote, it occurred to me that maybe I do something similar to that in some of my client-side code. In an application I'm working on, there are a bunch of unrelated ... read »
Jun 17, 2013 at 9:36 PM
Object Thinking By David West
@Jonah, Please, don't feel bad at all. I appreciate all that you have contributed to the conversation. And, the more points of view I get, the more confident I am that I will some day, some how und ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools