views:

125

answers:

3

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"/>
  -->
</appender>

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

+1  A: 

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

mkoeller
Or xsltproc, http://www.xmlsoft.org/XSLT/xsltproc2.html
divideandconquer.se
A: 

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.

Bevan
+3  A: 

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

Joseph Holsten