java 你能用 Ant 来构建/修改 XML 文件吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3064720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:10:39  来源:igfitidea点击:

Can you use Ant to Build/Modify XML files?

javaxmlant

提问by sarcasteak

I am quite new to ant and have been looking at the tasks. I am trying to generate an xml file. Do I need to call an external process or does ant have some way to do this? It could be as simple as sending a string to a txt file and saving it as a .xml. Is it possible?

我对 ant 很陌生,并且一直在研究这些任务。我正在尝试生成一个 xml 文件。我需要调用外部进程还是 ant 有办法做到这一点?它可以像将字符串发送到 txt 文件并将其保存为 .xml 一样简单。是否可以?

采纳答案by Craig Trader

The correct answer depends upon what you're actually trying to do.

正确答案取决于您实际尝试做什么。

You could create a tiny xml document using echotask and argument replacement, but that gets hard to maintain very quickly.

您可以使用echo任务和参数替换创建一个很小的 ​​xml 文档,但这很难很快维护。

If your goal is to generate an XML document that is mostly boilerplate with a couple of values substituted, then you should look at creating a template document and then using the Copy with filteringtask.

如果您的目标是生成一个 XML 文档,该文档主要是替换了几个值的样板文件,那么您应该考虑创建一个模板文档,然后使用带有过滤复制任务。

If you need to modify the structure of the document depending upon data from Ant (or gathered from somewhere else) then using the xslttask is going to be a better fit. The problem with XSLT is that it is not always straight-forward to use (XSLT uses functional programming, not procedural programming).

如果您需要根据来自 Ant 的数据(或从其他地方收集的数据)修改文档的结构,那么使用xslt任务将更合适。XSLT 的问题在于它并不总是直接使用(XSLT 使用函数式编程,而不是过程式编程)。

You may find that the correct answer is to write your own Ant taskthat will do exactly what you want, just the way you want it.

您可能会发现正确的答案是编写您自己的 Ant 任务,该任务将完全按照您的意愿执行您想要的操作。

If you can better describe what you're trying to achieve, I'm sure someone will be happy to provide a more precise answer.

如果您能更好地描述您要实现的目标,我相信有人会很乐意提供更准确的答案。

回答by Rebse

When you have to deal with any kind of XML processing within in your ant workflow, the xmltaskis strongly recommended. Very detailed documentation and good support. All you need beside that is some XPATH knowledge. Here => another helpful article

当您必须在 ant 工作流程中处理任何类型的 XML 处理时,强烈建议使用xmltask。非常详细的文档和良好的支持。除此之外,您只需要一些XPATH 知识。这里 =>另一篇有用的文章

回答by tonio

You can use the Replacetask to replace a given string in a template, and save it as xml. A simple example, as seen in the ant documentation:

您可以使用该Replace任务替换模板中的给定字符串,并将其保存为 xml。一个简单的例子,如ant文档中所见:

<replace file="${src}/index.html" token="@@@" value="wombat"/>

I used it to replace a constant @version@by the actual build identifier in a java project, for example.

例如,我用它来用@version@java 项目中的实际构建标识符替换常量。

If you want to do more complex processing, you should look at the XSLTtask. Foe example, to generate documentation, with the datereplace in the output, you can use something like:

如果你想做更复杂的处理,你应该看看XSLT任务。例如,要生成文档,使用date输出中的替换,您可以使用以下内容:

<xslt basedir="doc" destdir="build/doc"
      extension=".html" style="style/apache.xsl">
  <param name="date" expression="07-01-2000"/>
</xslt>

回答by Jordan

You could do that, although it seems to me more like something you would want to script beforehand, or something that you could call out to from Ant.

你可以这样做,尽管在我看来它更像是你想要事先编写脚本的东西,或者你可以从 Ant 调用的东西。

The task would be:

任务是:

<echo file="my.xml"><!--put escaped xml here--></echo>

This is of course going to be tedious since everything has to be escaped, but you can do it.

这当然会很乏味,因为一切都必须转义,但你可以做到。