ant在xml文件中执行或foreach循环的任务

时间:2013-07-18 13:31:57

标签: ant ant-contrib xmltask

我需要一些帮助来循环浏览一个xml文件,我使用xmlproperty来获取节点,但我正在努力研究如何在有多个参数的情况下循环它们。

所以这是格式:

<Problems>
      <Problem>
        <Rule>1</Rule>
        <ProblemDescription>1</ProblemDescription>
        <SourceFile>1</SourceFile>
        <Line>1</Line>
        <Column>1</Column>
        <Severity>Warning</Severity>
     </Problem>
     <Problem>
       <Rule>2</Rule>
       <ProblemDescription>2</ProblemDescription>
       <SourceFile>2</SourceFile>
       <Line>2</Line>
       <Column>2</Column>
      <Severity>Warning</Severity>
     </Problem>
</problems>

我想遍历这个,所以我可以得到以下输出:

1 1 1 1 1 1 2 2 2 2 2

解决方案:

  <target>
    <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>
      <xmltask source="problem.xml">

        <call path="/Problems/Problem">
            <param name="rule" path="Rule/text()" />
            <param name="probdesc" path="ProblemDescription/text()" />
            <actions>
                <echo>Rule: @{rule}</echo>
                <echo>Problem Description: @{probdesc}</echo>
            </actions>
            </call>
    </target>

2 个答案:

答案 0 :(得分:0)

您可以使用XPATH表达式返回与给定模式匹配的所有内容。在您的情况下,某些标签的值。

Here is an XPATH Ant task

答案 1 :(得分:0)

多年来没有使用ANT,因为我切换到了maven。

当我使用ant时,我会为这种功能创建一个自定义ant任务。然后你就拥有java的全部功能和更易读的代码,代价是在你需要进行更改时必须编译任务。

这实际上取决于你将如何处理输出。如果你做的事情非常简单,那么xpath的另一个答案更合适。

请参阅: http://ant.apache.org/manual/develop.html http://docs.oracle.com/javase/tutorial/jaxp/sax/parsing.html