Monday, June 1, 2009

Adding new data to XML using Coldfusion

Recently asked a Question 

"Need to create an XML file in which data is appended to existing XML"

Answer is pretty simple, first create  XML using cfxml tag

<cfprocessingdirective suppresswhitespace="Yes">
<cfxml variable="xmlobject">
        <name>Brijesh Chauhan</name>
        <name>Radhika Chauhan</name>

Then insert New XML nodes using array insert, we will always insert the new data at first and second position(as in this case there are two children's).

ArrayInsertAt(, 1, XmlElemNew(xmlobject,"name"));
ArrayInsertAt(, 2, XmlElemNew(xmlobject,"email"));

Finally assign new data to the childrens

<cfset[1].XmlText = "Avyukth Chauhan"> 
<cfset[2].XmlText = ""> 
</cfprocessingdirective > 

Simple !!!!

No comments: