bash script to edit xml file

Gabriel Solomon picture Gabriel Solomon · Oct 12, 2009 · Viewed 15.5k times · Source

I want to edit the config file of a program that is an XML:

<software>
   <settings>
       ...
       <setting name="local directory" type="string">/home/username/</setting>
       ...
   </settings>
</software>

What is the easiest way to do this from a bash script?

Thanks

Answer

lmxy picture lmxy · Jul 8, 2010

Using xmlstarlet:

xmlstarlet val -e file.xml
xmlstarlet ed -u "//settings/setting/@name" -v 'local directory2' file.xml
xmlstarlet ed -u "//settings[1]/setting/@name" -v 'local directory2' file.xml

# edit file inplace
xmlstarlet ed -L -u "//settings/setting/@name" -v 'local directory2' file.xml