2 Replies Latest reply on Feb 26, 2007 10:05 AM by Newsgroup_User

    Can I append an XML Document into another?

    alaskaDude
      I have a large xml document which I open into coldfusion (mx 7), and then a smaller group of files that are opened into thier own xml objects. After some customization of xml text values, I want to append the smaller xml documents into the larger document at specific positions within the document tree.

      I have the code that I thought would do this, using an arrayappend function, but what happens is that only the root element of the smaller (inserted) xml document is placed into the main document.

      Is this possible, or do I need to modify the code so that the entire tree of xml data that I am appending is created on the fly?
        • 1. Re: Can I append an XML Document into another?
          vollja
          Hi alaskaDude,

          sorry to not be able to provide a solution for you. Since I was looking for a solution for this problem myself. I know there was a way to do this in CF 6.1 by converting objects to Java and joining them in Java code. But this does not work in CF7 anymore. I haven't fiddled with the Java part since I'm not so much into it. There may be yet some solution possible.

          The solution for 6.1 is described here:
          http://www.spike.org.uk/blog/index.cfm?do=blog.entry&entry=B495C724-D565-E33F-3A31D0EE819F 1050

          Here some code that stuffs a xml object via string conversion into another one. Though I doubt that it's able to do some more sophisticated work. The <cfset> in the comment doesn't work although it would be great.

          Anyone else to offer a solution?

          • 2. Re: Can I append an XML Document into another?
            Level 7
            Personally I do this with XSLT.

            <cfxml variable="doc1">
            <root>
            <member id="10">
            <name>Ian</name>
            <sex>male</sex>
            </member>
            </root>
            </cfxml>

            <cfxml variable="doc2">
            <root>
            <member id="1">
            <name>Joe</name>
            <sex>male</sex>
            </member>
            <member id="2">
            <name>John</name>
            <sex>male</sex>
            </member>
            <member id="3">
            <name>Sue</name>
            <sex>female</sex>
            </member>
            </root>
            </cfxml>

            <cffile action="write" file="#expandPath("/")#\doc2.xml"
            output="#ToString(doc2)#" nameconflict="overwrite">

            <!--- METHOD ONE using a depreciated and undocumented method --->
            <cfset newNode = doc1.root.member.cloneNode(true)>
            <cfset doc2.changeNodeOwner(newNode)>
            <cfset doc2.root.appendChild(newNode)>

            <cfdump var="#doc2#" label="Merged by hidden functions" expand="no">

            <!--- METHOD TWO using XSLT --->
            <!--- create an xsl docutment--->
            <cfoutput>
            <cfsavecontent variable="transformer">
            <xsl:stylesheet xmlns:xsl=" http://www.w3.org/1999/XSL/Transform"
            version="1.0">
            <xsl:output method="xml"/>
            <!--load the merge file -->
            <xsl:variable name="emps" select="document('#expandPath("/")#\doc2.xml')"/>
            <!--- this line references and XML file to be combined with the main file,
            I wonder if there is a way to use an XML document in memory here? --->

            <!-- combine the files -->
            <xsl:template match="/">
            <root>
            <!-- select all the child nodes of the root tag in the main file -->
            <xsl:for-each select="root/child::*">
            <!-- copy the member tag -->
            <xsl:copy-of select="."/>
            </xsl:for-each>

            <!-- and all the child nodes of the root tag in the merge file -->
            <xsl:for-each select="$emps/root/child::*">
            <!-- copy the member tag -->
            <xsl:copy-of select="."/>
            </xsl:for-each>
            </root>
            </xsl:template>
            </xsl:stylesheet>
            </cfsavecontent>
            </cfoutput>

            <!--- create a combined xml document --->
            <cfset doc2 = XMLparse(XMLtransform(doc1,transformer))>

            <cfdump var="#doc2#" label="Merged by XSLT" expand="no">