I want to take an XML file and replace an element's value. For example if my XML file looks like this:
<abc>
<xyz>original</xyz>
</abc>
I want to replace the xyz element's original value, whatever it may be, with another string so that the resulting file looks like this:
<abc>
<xyz>replacement</xyz>
</abc>
How would you do this? I know I could write a Java program to do this but I assume that that's overkill for replacing a single element's value and that this could be easily done using sed to do a substitution using a regular expression. However I'm less than novice with that command and I'm hoping some kind soul reading this will be able to spoon feed me the correct regular expression for the job.
One idea is to do something like this:
sed s/\<xyz\>.*\<\\xyz\>/\<xyz\>replacement\<\\xyz\>/ <original.xml >new.xml
Maybe it's better for me to just replace the entire line of the file with what I want it to be, since I will know the element name and the new value I want to use? But this assumes that the element in question is on a single line and that no other XML data is on the same line. I'd rather have a command which will basically replace element xyz's value with a new string that I specify and not have to worry if the element is all on one line or not, etc.
If sed is not the best tool for this job then please dial me in to a better approach.
If anyone can steer me in the right direction I'll really appreciate it, you'll probably save me hours of trial and error. Thanks in advance!
--James