xml - Ant xmlproperty 任务。当有多个同名标签时会发生什么?

标签 xml ant

我正在尝试遵循已提供的大型 ant 构建文件,但在这种情况下我无法理解 xmlproperty 的功能。 考虑这个 xml 文件,example.xml。

<main>
  <tagList>
    <tag>
      <file>file1</file>
      <machine>machine1</machine>
    </tag>
    <tag>
      <file>file2</file>
      <machine>machine2</machine>
    </tag>
  </tagList>
</main>

在构建文件中,有一个任务可以简化为以下示例:

<xmlproperty file="example.xml" prefix="PREFIX" />

据我了解,如果只有一个 <tag>元素,我可以获得 <file> 的内容与 ${PREFIX.main.tagList.tag.file} 因为它大致相当于这样写:

<property name="PREFIX.main.tagList.tag.file" value="file1"/>

但是因为有两个<tag> s,${PREFIX.main.tagList.tag.file}的值是多少?在这种情况下?如果它是某种列表,我如何遍历 <file>值(value)观?

我使用的是 ant 1.6.2。

最佳答案

当多个元素同名时,<xmlproperty>创建具有逗号分隔值的属性:

<project name="ant-xmlproperty-with-multiple-matching-elements" default="run" basedir=".">
    <target name="run">
        <xmlproperty file="example.xml" prefix="PREFIX" />

        <echo>${PREFIX.main.tagList.tag.file}</echo>
    </target>
</project>

结果:

run:
     [echo] file1,file2

要处理逗号分隔值,请考虑使用 the <for> task来自第三方 Ant-Contrib 库:

<project 
    name="ant-xmlproperty-with-multiple-matching-elements" 
    default="run" 
    basedir="." 
    xmlns:ac="antlib:net.sf.antcontrib"
    >
    <taskdef resource="net/sf/antcontrib/antlib.xml" />
    <target name="run">
        <xmlproperty file="example.xml" prefix="PREFIX" />

        <ac:for list="${PREFIX.main.tagList.tag.file}" param="file">
            <sequential>
                <echo>@{file}</echo>
            </sequential>
        </ac:for>
    </target>
</project>

结果:

run:
     [echo] file1
     [echo] file2

关于xml - Ant xmlproperty 任务。当有多个同名标签时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16356605/

相关文章:

java - 在 eclipse 中调试注释处理器

tomcat - JBWEB000250 : Child container with name already exists

ant - 如何在 Ant 中从逗号分隔的目录列表创建文件集?

c# - 使用 linq 或 xpath 获取 xml 中的所有文本节点

ruby - 什么是 Ruby 的快速 XML 解析器?

java - StAX 不返回字符串中的所有字符

javascript - 为什么我得到一个 {"location": null} when I try to convert a text/xml into an object?

c# - 删除xml中的特定节点

java - AntClassLoader 无法转换为 URLClassLoader

java - Ant jar 任务仅包含单个类文件