|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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> |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ColdFusion Development > Need help with Coldfusion/XML errors |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|