Home Fabio Cortesi Stefan Jäger

Welcome to the Joint-Venture Blog from Fabio Cortesi and Stefan Jäger.

 

 
 

 

Ant offers some XML features like the xslt task, which transforms XML files into other files. But unfortunately, with Ant you can’t make simple XPath Queries on a specific XML file.

 

Luckily, there is a nice open source solution called XmlTask, which offers many possibilities to work with XML files.

 

  1. Download the latest version of XmlTask from http://xmltask.sf.net
  2. Create a task definition in Ant
    1
    2
    3
    4
    
    <taskdef 
      name="xmltask" 
      classname="com.oopsconsultancy.xmltask.ant.XmlTask"
      classpath="./xmltask-v1.15.1.jar" />
  3. Use it with the <xmltask> definition

    For example, read out the number of a specific node in the XML file:

    1
    2
    3
    
    <xmltask source="someFile.xml">
      <copy path="count(//aNode)" property="numberOfNodes" />
    </xmltask>

    Or change an attribute:

    1
    2
    3
    
    <xmltask source="someFile.xml" dest="toAnotherFile.xml">
      <attr path="//aNode[1]" attr="enabled" value="true"/>
    </xmltask>

 

There are many other possibilites to work with XML files inside Ant. Check out the documentation at http://xmltask.sf.net.

 

 

 

 

 
  1. If you are using Groovy in your Ant-build (as you have shown in a previous post), then you could also use Groovy for very expressive XML manipulation…

    http://groovy.codehaus.org/Processing+XML

    Comment by Jonas — 18.5.2009 @ 23:49

Leave a comment