Coldfusion XML, adding an XML node from another XML document object

473 views
Skip to first unread message

BarryC

unread,
Mar 3, 2010, 3:17:42 PM3/3/10
to cfaussie
Hi,

I've hunted around for a solution to this but haven't found a simple
or clean one (maybe there isn't one) but maybe someone here has some
suggestions.

I have two XML objects and I want to take an XML node from one and
insert it into the other

XML Object 1:
<?xml version="1.0" encoding="UTF-8"?>
<styles>
<style>
<property>background</property>
<value>#FFFFFF</value>
</style>
</styles

XML Object 2:
<?xml version="1.0" encoding="UTF-8"?>
<styles>
<style>
<property>border-color</property>
<value>#00A0AF</value>
</style>
</styles>

I want to take the <style> node in XML Object 2 and add/insert it into
XML Object 1 so I end up with this result:

<?xml version="1.0" encoding="UTF-8"?>
<styles>
<style>
<property>background</property>
<value>#FFFFFF</value>
</style>
<style>
<property>border-color</property>
<value>#00A0AF</value>
</style>
</styles>

You can't do a direct assignment like this:
<cfset
arrayAppend(xmlObj1.styles.XMLChildren,xmlObj2.styles.XMLChildren[1])>
as it complains with the message:WRONG_DOCUMENT_ERR: A node is used in
a different document than the one that created it.

There doesn't seem to be any simple examples on how to use
XmLElemNew() to create a copy of a node easily it looks like you'd
have to manually iterate through the element attributes and set them.

Thanks
Barry

Kym Kovan

unread,
Mar 3, 2010, 5:49:30 PM3/3/10
to cfau...@googlegroups.com
On 4/03/2010 07:17, BarryC wrote:
> Hi,
>
> I've hunted around for a solution to this but haven't found a simple
> or clean one (maybe there isn't one) but maybe someone here has some
> suggestions.
>
> I have two XML objects and I want to take an XML node from one and
> insert it into the other


Ben Nadel has some nice tools:

http://www.bennadel.com/blog/1532-Copy-Import-XML-Nodes-Into-A-ColdFusion-XML-Document.htm

we grabbed a bunch of his XML tools and other stuff we found and put
them in a XML utility CFC, it gets used lots :-)

--

Yours,

Kym Kovan
mbcomms.net.au

BarryC

unread,
Mar 4, 2010, 5:24:10 AM3/4/10
to cfaussie
Thanks,
yes I had stumbled across that in my hunting. Seems odd how there are
not some proper Coldfusion commands for that sort of stuff, it's like
they decided to make half the XML functions and then gave up.

On Mar 4, 11:49 am, Kym Kovan <dev-li...@mbcomms.net.au> wrote:
> On 4/03/2010 07:17, BarryC wrote:
>
> > Hi,
>
> > I've hunted around for a solution to this but haven't found a simple
> > or clean one (maybe there isn't one) but maybe someone here has some
> > suggestions.
>
> > I have two XML objects and I want to take an XML node from one and
> > insert it into the other
>
> Ben Nadel has some nice tools:
>

> http://www.bennadel.com/blog/1532-Copy-Import-XML-Nodes-Into-A-ColdFu...

amay0048

unread,
Apr 17, 2010, 4:18:08 AM4/17/10
to cfaussie
Coldfusion XML objects can be manipulated by the native coldfusion
struct and array methods, depending on the xml parts you wish to
manipulate.

Full list of native functions is here:
http://www.co.multnomah.or.us/cfdocs/Developing_ColdFusion_MX_Applications_with_CFML/XML7.html

On Mar 4, 6:17 am, BarryC <barrychester...@gmail.com> wrote:
> Hi,
>
> I've hunted around for a solution to this but haven't found a simple
> or clean one (maybe there isn't one) but maybe someone here has some
> suggestions.
>
> I have twoXMLobjects and I want to take anXMLnode from one and
> insert it into the other
>
> XMLObject 1:
> <?xmlversion="1.0" encoding="UTF-8"?>
> <styles>
>   <style>
>     <property>background</property>
>     <value>#FFFFFF</value>
>   </style>
> </styles
>
> XMLObject 2:
> <?xmlversion="1.0" encoding="UTF-8"?>
> <styles>
>  <style>
>     <property>border-color</property>
>     <value>#00A0AF</value>
>   </style>
> </styles>
>
> I want to take the <style> node inXMLObject 2 andadd/insert it intoXMLObject 1 so I end up with this result:
>
> <?xmlversion="1.0" encoding="UTF-8"?>
> <styles>
>   <style>
>     <property>background</property>
>     <value>#FFFFFF</value>
>   </style>
>  <style>
>     <property>border-color</property>
>     <value>#00A0AF</value>
>   </style>
> </styles>
>
> You can't do a direct assignment like this:
> <cfset
> arrayAppend(xmlObj1.styles.XMLChildren,xmlObj2.styles.XMLChildren[1])>
> as it complains with the message:WRONG_DOCUMENT_ERR: A node is used in
> a different document than the one that created it.
>
> There doesn't seem to be any simple examples on how to use
> XmLElemNew() to create a copy of a node easily it looks like you'd
> have to manually iterate through the element attributes and set them.
>
> Thanks
> Barry

--
You received this message because you are subscribed to the Google Groups "cfaussie" group.
To post to this group, send email to cfau...@googlegroups.com.
To unsubscribe from this group, send email to cfaussie+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cfaussie?hl=en.

Reply all
Reply to author
Forward
0 new messages