Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing XML Files With Cold Fusion 1

Status
Not open for further replies.

sarcus25

Programmer
Apr 14, 2005
27
Hi I'm Kind of new to cold Fusion, at the moment I'm trying to overwrite to an existing element in a Xml File. I can retrieve and view the element of my choice but I'm not sure how to Modify that element then write it back into the XML Ducument Object.
For Instance:


<!--- Convert file to XML document object --->
<cffile action="read" file="C:\CFusionMX7\ variable="myxml" charset="utf-8">
<cfset mydoc = XmlParse(myxml)>

<!---Extract INvoice number--->
<cfset InvoiceNum=#mydoc.ExternalOrder.Workstation#>


<!---Not sure how to write the "modified value" along with the rest of the elements into the file in the same order. For example if the user changes InvoiceNum to 4 then how do I put this value back into the "mydoc" XML object --->




<!---Converting the XML object back into string then save to a file--->
<cfset Xmltext = tostring(mydoc)>
<cffile action="write" file="C:\CFusionMX7\ output="#Xmltext#">
 
When you parse the xml string, the basic string object is basically turned into an array in memory, meaning that you can change the values using the normal . syntax.

So, if you want to be able to change the value of the element Workstation, then this should work:

<cfset mydoc.ExternalOrder.Workstation = 6>

then what you have for the writing back to the file looks fine to me, but to save a step you can put the ToString function within the cffile, like this:

<cffile action="write" file="C:\CFusionMX7\ output="#ToString(MyDoc)#">

Also, just as a side point, it's never really a good idea to have hard coded file paths. Have a look at the ExpandPath() function, it will allow you to write to a file relevant to where you are executing from!

Hope this helps!

Tony
 
Thanks alot for the help, that's just the answer I was looking for. Star for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top