How to parse xml file in Ant Script using xmlproperty task [example]

Sometimes we need to parse XML file using Ant-script to read some property value or run the java file and more like this. It’s very easy, we can do this with tag. This tag (xmlproperty) loads the xml file and it convert all the values of xml file in ant property value internally and we can use those value our script as ant property. For example :


is roughly equivalent to this into ant-script file:

and you can print this value with ${}.

An Example of xmlproperty task:
Step 1: Create a xml file named books.xml and save this file in xmlproperty-demo directory.

<?xml version="1.0" encoding="UTF-8"?>
	<book id="bk-101">
		<author>Karan Malhotra</author>
		<title>Mastering Ant</title>

Step 2: Create an ant script named ant-build.xml and save this file in xmlproperty-demo directory.


<?xml version="1.0" encoding="UTF-8"?>

<project name="xmlproperty-demo" default="init">
	<xmlproperty file="books .xml" collapseAttributes="true" />

	<target name="init">
		<echo> Book ID :: ${} </echo>
		<echo> Author :: ${} </echo>
		<echo> Title :: ${} </echo>
		<echo> Price :: ${} </echo>


Step 3: Go to xmlproperty-demo directory and issue this command:

ant -f ant-build.xml

You will get output as below:

If you have multiple records in xml file then it will show all record with comma separated like this:

How to use for loop in Ant

In the previous article, I have discussed about ant-contrib extensions. In this article I am going to talk to you about how to use for loop in ant. I would suggest you to go through the earlier one for better understanding.

Ant doesn’t directly support for loop. However, you can get the ant-contrib extensions, which can be used for this purpose.
To understand how to use for loop in ant-script create a java project in eclipse and make sure that ant-contrib is configured properly. Create a build.xml in this project. See the given image.

Copy the code below in build.xml of  your AntProject and run this build.xml as Ant Build.


<?xml version="1.0" encoding="UTF-8"?>  
<project name="AntProject" default="test" basedir="." xmlns:ac="antlib:net.sf.antcontrib">
   <target name="test" >  
    <echo message=" For task demonstration "/>  
       <property name="count" value="1"/>
    <ac:for list="Printing all words in this sentence"  delimiter=" " param="word" 
        trim="false" parallel="false">                             

Output :

Buildfile: F:\java_program\AntProject\build.xml
[echo] For task demonstration
[echo] ***Printing***
[echo] ***all***
[echo] ***words***
[echo] ***in***
[echo] ***this***
[echo] ***sentence***
Total time: 265 milliseconds
The for task iterates over a list, a list of files or directories.To substitute value of a parameter @{} notation is used . For task requires ant version 1.6.0 or higher.

Explanation of above ‘for’ task

  • list attribute is mandatory and is used to specify a list of values to iterate over.
  • param attribute is used to hold the values of the list and is mandatory.
  • delimeter attribute specifies the character by which values are separated e.g. the space character ‘ ‘ is delimiter in our case. This is optional attribute .By default comma ‘,’ act as delimiter in ant.
  • parallel attribute is set to false by default and forces sequential execution of the iterations. If this attribute is set to true it will cause parallel execution of nested tag; it means order of execution of iterations is unpredictable. Order of printing characters will be different in each execution of build.xml, just like threads executing in parallel.
  • trim attribute is false by default. If true it will remove any leading or trailing whitespace from the list item before it is passed to the sequential.

Ant-Contrib extensions

The Ant-Contrib plugin is a collection of tasks for Apache-Ant. Basic Ant tasks were lacking some of functionality like conditional statements, looping. Antcontrib provides a collection of tasks such as conditional statements and operations on properties as well as other useful tasks. We can use Ant-Contrib tasks by copying antcontrib( (1.9 MB)) jar to ant’s lib directory or by adding jar to CLASSPATH. Once this is done, antcontrib tasks can be invoked directly in the typical build.xml file.
Ant-Contrib jar can be downloaded from, Generally Ant-Contrib tasks require Ant 1.5 or higher version to work properly.

Configuring Ant-Contrib in eclipse

  • Download ant-contrib-1.0b3.jar from the above URL
  • Add it to classpath used by eclipse while executing build file
  • Select Windows > Preferences > Ant > Runtime > Ant Home Entries > Add External Jars give path to ant-contrib jar and click open.

Simple Ant Project in Eclipse

  • Open eclipse and create a new java project                                                                                                                                 Select  File > new > java project  as shown in fig below.

  • A dialogue box will appear , write the project name as “AntProject” in the text field of the dialogue box as shown in fig below and click Finish button.

You will se a new project in package explorer on your eclipse.

  • Now right click the newly created project and create a class in this project as                  Select  new > class  as in fig below                                                                                                                                                                                                                                                                                                                                                   

Fill in the details in the dialogue box as..                                                                                       Create a public class named HelloWorld in package pack1                                                          Click the finish button.  See fig below

Ant Installation and Configuration

1.1 Ant Installation

  • Download Apache Ant from .
  • Extract the zip file into a directory location of  your choice, e.g. Extract it in F: drive, F:\apache-ant-1.8.2 .  This is ant installation directory and we will refer this directory as “ANTPACKAGE” in our example.
  • Set the “PATH” environment variable to include the “ANTPACKAGE/bin” directory in your path. e.g. PATH= ” F:\apache-ant-1.8.2/bin” .
  • Set the “CLASSPATH” environment variable to include the “ANTPACKAGE/lib” directory in your path. e.g. CLASSPATH= ” F:\apache-ant-1.8.2\lib” .

To check installation run the following command

  ant  –version                                                                                                                                                                                                                 It will show the following message.

1.2 Make sure that you have JDK and eclipse installed properly.

  • Set JAVA_HOME to point to JDK installation directory.
  • Set PATH to include JAVA__HOME/bin directory.
  • Set CLASSPATH to include JAVA__HOME/lib directory