使用ant迭代int xml文件

时间:2012-07-17 18:31:27

标签: xml parsing ant

我有一个带有下面结构的Xml文件。我想按节点读取节点并使用没有commom分隔符的值调用特定任务。在Ant中这可能吗?

<projects>
<!-- first node -->
 <project>
    <name> </name>
    <url> </url>
    <package> </package>
 </project>

 <!-- Second node -->
 <project>
    <name> </name>
    <url> </url>
    <package> </package>
 </project>
<projects>

谢谢!

3 个答案:

答案 0 :(得分:1)

使用XSLT任务将输入XML文件处理为随后执行的ANT脚本。

实施例

|-- build.xml
|-- projects-process.xsl
`-- projects.xml

Running将处理

中的信息
$ ant
Buildfile: /home/mark/tmp/build.xml

run-projects:
     [xslt] Processing /home/mark/tmp/projects.xml to /home/mark/tmp/build-tmp.xml
     [xslt] Loading stylesheet /home/mark/tmp/projects-process.xsl

build:

dosomething:
     [echo] DOSOMETHING: 'Project 1' 'http://someurl1' 'project1'"

dosomething:
     [echo] DOSOMETHING: 'Project 2' 'http://someurl2' 'project2'"

BUILD SUCCESSFUL

projects.xml

<projects>

    <project>
        <name>Project 1</name>
        <url>http://someurl1</url>
        <package>project1</package>
    </project>

    <project>
        <name>Project 2</name>
        <url>http://someurl2</url>
        <package>project2</package>
    </project>

</projects>

项目-process.xsl

此XSLT样式表用于生成ANT脚本

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <project name="genbuild" default="build">

            <target name="build">
                <xsl:apply-templates select="projects/project"/>
            </target>

            <target name="dosomething">
                <echo>DOSOMETHING: '${name}' '${url}' '${package}'"</echo>
            </target>

        </project>
    </xsl:template>

    <xsl:template match="project">
        <antcall target="dosomething">
            <param name="name"    value="{name}"/>
            <param name="url"     value="{url}"/>
            <param name="package" value="{package}"/>
        </antcall>
    </xsl:template>

</xsl:stylesheet>

的build.xml

运行XSLT转换以处理 projects.xml 文件并生成ANT构建文件

<project name="demo" default="run-projects">

    <target name="run-projects">
        <xslt style="projects-process.xsl" in="projects.xml" out="build-tmp.xml"/>

        <ant antfile="build-tmp.xml"/>
    </target>

    <target name="clean">
        <delete file="build-tmp.xml"/>
    </target>

</project>

答案 1 :(得分:0)

Ant项目不能嵌套在同一个Ant构建文件中。但是,构建逻辑可以使用MacroDefs进行模块化。此外,子项目可以拥有自己的构建文件,可以使用Ant Ant任务从主构建文件中调用它们。可以使用AntCall task在同一构建文件中调用Ant目标。

答案 2 :(得分:0)

thiagolsilva,

使用xmlproperty和javascript的组合我能够拆分projects.xml文件的多元素(相同节点)并在ant中使用项目节点数据字段。所有操作都在一个ant文件中执行,只需执行一次。

上面示例中的示例数据projects.xml文件:

<projects>
    <project>
        <name>Project 1</name>
        <url>//someurl1</url>
        <package>project1</package>
    </project>
    <project>
        <name>Project 2</name>
        <url>//someurl2</url>
        <package>project2</package>
    </project>
</projects>

必须转换为以下格式的projects.xml文件:

<myProjects>
    <!-- name;url;package -->
    <myProject>Project 1;//someurl1;project1</myProject>
    <myProject>Project 2;//someurl2;project2</myProject>
</myProjects>

此处,先前的节点元素被转换为单个字符串,其中字段由“;”字符分隔。

我已将元素“project”重命名为“myProject”以避免混淆,因为我将在javascript中使用ant“project”对象。

一旦我们将可重复的xml节点扁平化为字符串,我们就可以使用以下的ant脚本来遍历元素,读取展平的属性并按照我们的意愿使用它们。

<project name="testProjects" default="ProcessProjects" basedir=".">

  <target name="init" >
  <!-- From: http://sourceforge.net/projects/ant-contrib/files/ant-contrib/ 
     download the jar and put it a created "lib" folder of your project, where ant file is.
  -->
    <taskdef resource="net/sf/antcontrib/antlib.xml">
      <classpath><pathelement location="./lib/ant-contrib-1.0b3.jar"/></classpath>
    </taskdef>
    <xmlproperty file="./projects.xml" keepRoot="false"/>

    <!-- Properties will be used to store the values of: name;url;package -->
    <property name="name" value=""/>
    <property name="url" value=""/>
    <property name="package" value=""/>
  </target>

  <target name="ProcessProjects" depends="init">
    <echo>Process Projects</echo>
    <foreach list="${myProject}" target="processLoopStep" param="var1"/>
  </target>

  <target name="processLoopStep" >
    <script language="javascript"> <![CDATA[
      var var1 = project.getProperty("var1");
      var parts = var1.split(";");
      project.setProperty("name",parts[0]);
      project.setProperty("url",parts[1]);
      project.setProperty("package",parts[2]);
      ]]>
    </script>

    <!-- Do what you like with the values of the <project> element -->
    <echo message="myProject name = ${name} url = ${url} package = ${package}"/>
  </target>

</project>

以下文件:   - C:\ temp \ lib \ ProcessProjects.xml - 构建   - C:\ temp \ lib \ projects.xml - 数据   - C:\ temp \ lib \ ant-contrib-1.0b3.jar - lib jar

输出:

C:\temp>ant -f ProcessProjects.xml

Buildfile: C:\temp\ProcessProjects.xml
init:
ProcessProjects:
     [echo] Process Projects
processLoopStep:
     [echo] myProject name = Project 1 url = //someurl1 package = project1
processLoopStep:
     [echo] myProject name = Project 2 url = //someurl2 package = project2
BUILD SUCCESSFUL
Total time: 0 seconds

此致,Jurek