#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    2
    Rep Power
    0

    Need help with Coldfusion/XML errors


    Hello,
    I am new to Cold Fusion and I thought someone could help me with this.

    What I want to do is add/insert an employee element into this xml document "test.xml" :


    <?xml version="1.0" encoding="UTF-8"?>
    <employees>
    <employee ID="1" FIRSTNAME="John" LASTNAME="Doe"/>
    <employee ID="2" FIRSTNAME="Donald" LASTNAME="Duck"/>
    <employee ID="3" FIRSTNAME="Micky" LASTNAME="Mouse"/>
    <employee ID="4" FIRSTNAME="Minnie" LASTNAME="Mouse"/>
    </employees>


    Here is the Cold Fusion code I have:

    <CFSCRIPT>
    //create object and initialize root element
    xmlEmp = XMLNew();
    xmlEmp.xmlRoot = XMLElemNew(xmlEmp, "employees");
    //add first child element
    xmlEmp.xmlRoot.xmlChildren[1] = XMLElemNew(xmlEmp, "employee");
    xmlEmp.xmlRoot.xmlChildren[1].XMLAttributes.id = "5";
    xmlEmp.xmlRoot.xmlChildren[1].XMLAttributes.firstname = "Daffy";
    xmlEmp.xmlRoot.xmlChildren[1].XMLAttributes.lastname = "Duck";
    </CFSCRIPT>
    <CFSET XML = #ToString(variables.xmlEmp)#>

    <CFFILE ACTION="Append" FILE="#getDirectoryFromPath(getTemplatePath())#test.xml" OUTPUT="#XML#" ADDNEWLINE="YES">


    This is what I get when this code gets proccessed :

    <?xml version="1.0" encoding="UTF-8"?>
    <employees>
    <employee ID="1" FIRSTNAME="John" LASTNAME="Doe"/>
    <employee ID="2" FIRSTNAME="Donald" LASTNAME="Duck"/>
    <employee ID="3" FIRSTNAME="Micky" LASTNAME="Mouse"/>
    <employee ID="4" FIRSTNAME="Minnie" LASTNAME="Mouse"/>
    </employees>
    <?xml version="1.0" encoding="UTF-8"?>
    <employees>
    <employee ID="5" FIRSTNAME="Daffy" LASTNAME="Duck"/>
    </employees>


    All I want it to do is do this:

    <?xml version="1.0" encoding="UTF-8"?>
    <employees>
    <employee ID="1" FIRSTNAME="John" LASTNAME="Doe"/>
    <employee ID="2" FIRSTNAME="Donald" LASTNAME="Duck"/>
    <employee ID="3" FIRSTNAME="Micky" LASTNAME="Mouse"/>
    <employee ID="4" FIRSTNAME="Minnie" LASTNAME="Mouse"/>
    <employee ID="5" FIRSTNAME="Daffy" LASTNAME="Duck"/>
    </employees>


    Can anyone help me with this problem? If so thanks a lot.
  2. #2
  3. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,243
    Rep Power
    967
    You can use the xmlMerge() function available at cflib.org to merge 2 XML objects together:

    <cfsavecontent variable="xmlString"><root><user>Brian</user><user>Todd</user><user>Bill</user></root></cfsavecontent>
    <cfset xmlObj = xmlParse( xmlString ) />

    <cfsavecontent variable="xmlString2"><root><user>Heather</user></root></cfsavecontent>
    <cfset xmlObj2 = xmlParse( xmlString2 ) />

    <cfset xmlMerge( xmlObj, xmlObj2, 'false' ) />
    <cfdump var="#xmlObj#">
    <cfoutput>#htmlCodeFormat( toString( xmlObj ) )#</cfoutput>

IMN logo majestic logo threadwatch logo seochat tools logo