Tuesday, October 9, 2012

How to parse XML file in ANT Script, read xml file in Ant, how to use

Some time we need to parse xml file using 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.xmlinit:
[echo] Student Name :: Binod Kumar Suman
[echo] Roll :: 110
[echo] City :: Bangalore
BUILD SUCCESSFULTotal 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.xmlinit:

[echo] Student Name :: Binod Kumar Suman,Pramod Modi,Manish Kumar
[echo] Roll :: 110,120,130
[echo] City :: Bangalore,Japan,Patna

BUILD SUCCESSFULTotal time: 109 milliseconds

Now if you want to get indiviual data without comma seperation then we have to use <foreach> tag in ant script and for that we have to add one more jar file ant-contrib-0.6.jar in lib folder.

Complete Example:
1. One xml file say StudentsInfo.xml (C:\XML_Workspace\XML_ANT\src\StudentsInfo.xml)
2. One ant script say Binod.xml (C:\XML_Workspace\XML_ANT\src\Binod.xml)
3. Put jar ant-contrib-0.6.jar in C:\XML_Workspace\XML_ANT\lib, you can download from here.

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

<Student>
<name>Binod Kumar Suman</name>
<roll>110</roll>
<city> Bangalore </city>
</Student>

<Student>
<name>Pramod Modi</name>
<roll>120</roll>
<city>Japan</city>
</Student>

<Student>
<name>Manish Kumar</name>
<roll>130</roll>
<city>Patna</city>
</Student>

</Students>

Binod.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="test" default="for-each">
<xmlproperty file="StudentsInfo.xml" collapseAttributes="true"/>

<target name="init">
<property name="ant-contrib.jar" location="C:/XML_Workspace/XML_ANT/lib/ant-contrib-0.6.jar"/>
<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="${ant-contrib.jar}"/>
</target>

<target name="for-each" depends="init">
< echo> TEST FOR EACH </echo>
<foreach list="${Students.Student.name}" target="loop" param="var" delimiter=","/>
</target>

<target name="loop">
<echo message="Name :: ${var}"/>
</target>

</project>

After run the Binod.xml ant script, output will be

Buildfile: C:\XML_ANT_Workspace\XML_ANT\src\Binod2.xmlinit:for-each:
[echo] TEST FOR EACH loop:
[echo] Name :: Binod Kumar Sumanloop:
[echo] Name :: Pramod Modiloop:
[echo] Name :: Manish Kumar
BUILD SUCCESSFULTotal time: 219 milliseconds

0 comments:

Post a Comment