从脚本调整XML配置文件

时间:2021-11-16 12:39:21

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

我正在努力自动配置几个JBoss服务器,这涉及编辑大量的XML文件。

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?

我想尽可能地编写所有这些更改的脚本。但是“标准”工具(sed,grep等)与XML不兼容。没有必要使用更高级别的语言,我如何编写脚本,例如在特定文件中的给定XML元素之后插入给定的XML snipper?

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

比如说我的jboss-log4j.xml看起来像

<!-- ====================== -->
<!-- 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?

并且我想添加一个新的appender-ref元素。从脚本中最简单的方法是什么?

3 个解决方案

#1


3  

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

你会在我之前的问题中找到更多答案。 xmlstar似乎是最受欢迎的答案。

#2


1  

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

通常我会通过编写XSL样式表并从脚本调用SAXON来完成此操作。

#3


0  

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.

Ant的.NET表兄NAnt有XmlPeek和XmlPoke任务,我在编辑非常复杂的WCF配置文件时已经习惯了这些任务。如果您可以找到Ant的类似任务,那么您可能会有一个胜利者。

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.

一个思想 - Ant有一个过滤链的想法,它可以用来在复制时转换文件 - NAnt有相同的概念,我最近用它来很好地配置部署文件。

#1


3  

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

你会在我之前的问题中找到更多答案。 xmlstar似乎是最受欢迎的答案。

#2


1  

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

通常我会通过编写XSL样式表并从脚本调用SAXON来完成此操作。

#3


0  

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.

Ant的.NET表兄NAnt有XmlPeek和XmlPoke任务,我在编辑非常复杂的WCF配置文件时已经习惯了这些任务。如果您可以找到Ant的类似任务,那么您可能会有一个胜利者。

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.

一个思想 - Ant有一个过滤链的想法,它可以用来在复制时转换文件 - NAnt有相同的概念,我最近用它来很好地配置部署文件。