Skip to main content
Participating Frequently
June 29, 2011
Answered

Modifying value in external xml file

  • June 29, 2011
  • 2 replies
  • 1568 views

Hi,

I've been searching around for some time and haven't been able to find the solution to what I think should be a simple problem.

I have an xml file that I need to change the value of an item from False to True.

The structure of the file is:

<data>

<system-page>

<id>1</id>

<other fields></other fields>

<dynamic-metadata>

     <name>publish</name>

     <name>yes</name>

</dynamic-metadata>

<dynamic-metadata>

     <name>frontpage</name>

     <name>no</name>

</dynamic-metadata>

</system-page>

<!---tons more system pages--->

</data>

I am able to read the file and get to the exact line as such:

<cffile action="read" file="E:\test.xml" variable="newsFile" charset="utf-8"/>
<cfset newsXML=XmlParse(newsFile)/>
<cfset arrItemNodes = XmlSearch(newsFile,"/data/system-page[@id='1']/dynamic-metadata[name='frontpage']/value")/>  

I'm not sure how to edit and save the value. I imaging it's something like this:

<cfset arrItemNodes[1].xmlText = 'True'>


But is there anyway to save this information in the xml file at the right location?

Any help is appreciated.

This topic has been closed for replies.
Correct answer -__cfSearching__-

I just found out that my issue is a little deeper. The XML is getting republished from a CMS. I'll have to go into the CMS and edit the file in there. Thank you for your patience and help though.


Haha. Well if you ever need to do this in the future, now you know how ;-)


Cheers

2 replies

Inspiring
June 29, 2011

BTW: You may want to double check your

XmlSearch expression too. I suspect it may not return the correct node. 
Inspiring
June 29, 2011
I'm not sure how to edit and save the value. I imaging it's something like this:
<cfset arrItemNodes[1].xmlText = 'True'>

I have not actually tested it, but your logic sounds right.  So give it a whirl. Just use ToString( ...) to convert the xml document back into a string. Then save it to disk.

http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec133ba-7ff6.html#WSc3ff6d0ea77859461172e0811cbec133ba-7fde

robomdAuthor
Participating Frequently
June 29, 2011

Thanks for the quick response. That's helpful, but how would I use ToString to only replace the corresponding section within the XML file and not replace then entire file with only that section. I have the following:

<cfset XMLText=ToString(arrItemNodes)> 
<cffile action="write" file="E:\test.xml" output="#XMLText#">

But this gives me an error.

I also tried ToString(newsXML) but this is essentially just copying the file back in without making any edits.

Inspiring
June 29, 2011

Exactly what code did you use and what was the full error message? Also are you on CF8 or 9?

My thinking was "arrItemNodes" should be a reference. So assuming you a) have the correct node and b) can modify it (I did not test it), any changes should be reflected in the root xml document as well. Then you could ToString(newsXML).