Some time we need to parse xml file in Ant script to run the java file or read some property value and more like this.
It is very easy, we can do this with tag called <xmlproperty>
. This tag loads the xml file and it convert all the values of xml file in ant property value internally and we can use those value as ant property. For example
<root>
<properties>
<foo>bar</foo>
</properties>
</root>
is roughly equivalent to this into ant script file as:
<property name="root.properties.foo" value="bar"/>
and you can print this value with ${root.properties.foo}.
Complete Example:
1. Create one xml file say Info.xml
2. Create one ant script say Check.xml
Info.xml
<?xml version="1.0" encoding="UTF-8"?>
<Students>
<Student>
<name>Binod Kumar Suman</name>
<roll>110</roll>
<city> Bangalore </city>
</Student>
</Students>
Check.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="Check" default="init">
<xmlproperty file="Info.xml" collapseAttributes="true"/>
<target name = "init">
<echo> Student Name :: ${Students.Student.name} </echo>
<echo> Roll :: ${Students.Student.roll} </echo>
<echo> City :: ${Students.Student.city} </echo>
</target>
</project>
Now after run this (Check.xml) ant script, you will get output
Buildfile: C:\XML_ANT_Workspace\XML_ANT\src\Check.xml
init:
[echo] Student Name :: Binod Kumar Suman
[echo] Roll :: 110
[echo] City :: Bangalore
BUILD SUCCESSFUL
Total time: 125 milliseconds
It was very simple upto here, but if you have multiple records in xml (StudentsInfo.xml) then it will show all record with comma seperated like this
Buildfile: C:\XML_ANT_Workspace\XML_ANT\src\Check.xml
init:
[echo] Student Name :: Binod Kumar Suman,Pramod Modi,Manish Kumar
[echo] Roll :: 110,120,130
[echo] City :: Bangalore,Japan,Patna
BUILD SUCCESSFUL
Total time: 109 milliseconds
Link