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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »