Adjusting XML config files from a script

I'm working on automating the configuration of several JBoss servers, which involves editing a substantial number of XML files.

I'd like to script all these changes as much as possible. But the "standard" tools (sed, grep et al) do not work well with XML. Without necessarily resorting to a higher-level language, how can I script e.g. the insertion of a given XML snipper after a given XML element in a certain file?

Say for instance that my jboss-log4j.xml looks like

<!-- ====================== -->
<!-- More Appender examples -->
<!-- ====================== -->

<!-- Buffer events and log them asynchronously -->
<appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
  <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
  <appender-ref ref="FILE"/>
  <appender-ref ref="CONSOLE"/>
  <appender-ref ref="SMTP"/>

and that I want to add a new appender-ref element. What's the easiest way to do that from a script?


You'll find more answers in my previous question. xmlstar seems to be the most popular answer.

Typically I'll do this by writing a XSL stylesheet and invoking SAXON from a script.

NAnt, the .NET cousin of Ant, has XmlPeek and XmlPoke tasks that I've used to very good effect in editing WCF configuration files which are quite complex. If you can find similar tasks for Ant, then you might have a winner.

Alternatively, another approach might be to have a "template" version of the configuration file that contains %placeholders% suitable for replacing with one of the more classic text processing tools.

A thought - Ant has the idea of a filter chain, which can be used to transform a file while copying - NAnt has the same concept and I've recently used that to good effect configuring deployment files.

Need Your Help

mysql can not compare data and parse it with php

php mysql md5

i'm getting back to make some php code to make a login/register form.

Efficient maths algorithm to calculate intersections

algorithm math performance intersection lines

For a game I am developing I need an algorithm that can calculate intersections. I have solved the problem, but the way I have done it is really nasty and I am hoping someone here might have a more