Not yet rated

Problem

Suppose I have a XML file: <?xml version="1.0"?> <person> <name> <firstname>John</firstname> <lastname>Barrett</lastname> </name> </person> Now I want to add another name to this XML file. How can I create a CF form (I am fine with creating the form) and post the content of that form (user input) to update the xml file?

Solution

A new record can be inserted by first reading the xml file to a varaible using cffile. The new record may then be inserted using ArrayAppend and XmlElemNew functions before finally writing it to file using cffile again.

Detailed explanation

  <!--- Collect the values to be inserted nto the new record using form --->

<cfset newBrandID1 = 111>

<cfset newBrandName1 = "name1">

<cfset newBrandImage1 = "image1">

<cfset newBrandID2 = 222>

<cfset newBrandName2 = "name2">

<cfset newBrandImage2 = "image2">
 
<!--- Read and parse the existing XML file and find it's length. --->
<cffile action="read" file="#Expanded file path\xml filename#"
variable="xmlFile">

<cfset myXml = XmlParse(xmlFile)>

<cfset myXmlLength = ArrayLen(myXml.brands.xmlChildren)>
 
<!--- For the next step, two methods are possible --->
<!--- Method 1: Create each child node one by one as shown below --->
<cfset myXml.brands.XmlChildren[#myXmlLength# + 1] =
XmlElemNew(myXml,"brand")>

<cfset myXml.brands.XmlChildren[#myXmlLength# +
1].XmlChildren[1] = XmlElemNew(myXml,"brandid")>

<cfset myXml.brands.XmlChildren[#myXmlLength# +
1].XmlChildren[2] = XmlElemNew(myXml,"brandname")>

<cfset myXml.brands.XmlChildren[#myXmlLength# +
1].XmlChildren[3] = XmlElemNew(myXml,"brandimage")>
<!--- Method 2: Copy an existing node as the last node and all child nodes will be automatically created. Can cause data errors --->
<cfset myXml.brands.XmlChildren[#myXmlLength# + 2] =
myXml.brands.XmlChildren[#myXmlLength#]>
 
<!--- Assign values to each child individually --->
<cfset myXml.brands.XmlChildren[#myXmlLength# +
1].brandid.xmltext = #newBrandID1#>

<cfset myXml.brands.XmlChildren[#myXmlLength# +
1].brandname.xmltext = #newBrandName1#>

<cfset myXml.brands.XmlChildren[#myXmlLength# +
1].brandimage.xmltext = #newBrandImage1#>

<cfset myXml.brands.XmlChildren[#myXmlLength# +
2].brandid.xmltext = #newBrandID2#>

<cfset myXml.brands.XmlChildren[#myXmlLength# +
2].brandname.xmltext = #newBrandName2#>

<cfset myXml.brands.XmlChildren[#myXmlLength# +
2].brandimage.xmltext = #newBrandImage2#>
 
<!--- Write output to file --->
<cffile action="write" file="#Expanded file path\xml filename#l"
output="#tostring(myXml)#">
 
<!--- The ArrayAppend function can also be used to insert data into xml files in which case, the number of records need not be found --->
<!--- The ArrayAppend syntax is <cfset ArrayAppend(myXml.brands.XmlChildren, XmlElemNew(myXml,"brandid"))> --->
 
 
www.toobler.com
 

 


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes