After battling with similar problems of editing XML configs, I wrote the freely available Ant plugin task XmlTask ( http://www.oopsconsultancy.com/software/xmltask ).
This permits easy property substitution and automated editing of XML configs whilst maintaining XML-correctness and validity (e.g. character encodings, entity encoding etc.)
You can go further and insert or remove subsections of XML as you require.
It was originally designed for J2EE configurations, but it's certainly suitable for other problem domains, such as the Spring configs shown here.