Sunday, June 21, 2009

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

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

22 comments:

  1. Good Posting. This same example we can do using xmltask tag in ant script. Please write one blog on xmltask as I searched on net but could not get.
    Thanks in advacne.
    Jack :)

    ReplyDelete
  2. Yes Jack, you are right. Using xmltask you can get the same result as we got from xmlproperty tag. On your request I posted article on xmltask. This is link to access that post.

    http://binodsuman.blogspot.com/2009/06/how-to-read-xml-file-and-extract-values.html

    Keep watching my blog and your any suggestion woule be most welcome to improve this blog. :)

    Thanks,

    Binod Suman

    ReplyDelete
  3. How would you print the roll and the other details?

    ReplyDelete
  4. Exactly what I was looking for. Thanks for the information.

    ReplyDelete
  5. Great Post.

    It was exactly what I was needing to my project. =)

    ReplyDelete
  6. Hi Binod,

    This is interesting . I've have a requirement wherein i need to get the output in this form

    Student::Name:Binod Kumar Suman,Roll:130,city:bangalore

    Student::Name:Pramod Modi,Roll:200,City:kolkata

    Student::Name:xyz,roll:90,city:japan

    Can we achieve this?How? Because the 1st approach prints all names , all roll, all cities etc, And 2nd approach individually parses through this. Can you please suggest a solution

    ReplyDelete
  7. Hi suman, This is really excellent work. I am using your script in my project. There is one thing i am not able to fetch. In your studentinfo.xml file there is info about each student. What i want to do is that to fetch the information for that student do some task and so forth for all the entries.

    Like a kind of IF condition based on the student name. Fetch that student details (roll number etc).

    Thanks in advance,
    Regards,
    Rohan

    ReplyDelete
    Replies
    1. Hi.... Have you done your above requirement. i have a similar kind of requirement i want to modify the student details based in student name can you please suggest me.

      Delete
  8. Thank you so much. This perfectly suited my need.

    ReplyDelete
  9. Hi suman,
    I am starting with ANT and antform scripting. I have one question, i have one xml file which contains properties like :
    '


    "Villa"
    "F3"


    "appart"
    "F4"


    '
    and i use antform to display a SelectProperty containing all House name like :


    I would like to use the property "HouseSelected" to display in TextProperty the associated House information (Description and type) of the selected object.

    Thanks in advance.

    ReplyDelete
  10. Hi suman,
    I am starting with ANT and antform scripting. I have one question, i have one xml file which contains properties like :

    sorry for the text format but all tags are not displayed if i let "<>"

    properties.xml

    Houses
    House Name="House1"
    Type "Villa" /Type
    Description "F3" /Description
    /House
    House Name="House2"
    Type "appart" /Type
    Description "F4" /Description
    /House
    /Houses

    and i use antform in my build.xml to display a SelectProperty containing all House name like :

    build.xml

    target name="House_menu" depends="loadDep"
    antform title="House Selection"
    selectionProperty label="House :" property="HouseSelected" values="${Houses.House.Name}"/
    Label House Desctiption : ${${HouseSelected}.Description}/Label
    Label House Type : ${${HouseSelected}.type}/Label

    /antform

    /target

    I would like to use the property "HouseSelected" to display in Label the associated House information (Description and type) of the selected object.

    Thanks in advance.

    ReplyDelete
  11. ${Students.Student.name} output [echo] Name :: Binod Kumar Suman, it runs only first index not all ... need help

    ReplyDelete
  12. Hi Suman,

    How to execute .bat and .sh files in ant script?

    Regards,
    Sri

    ReplyDelete
  13. Hi Suman,

    How to create xml files using Ant script?Is there a task to do it?

    Regards,
    Sri

    ReplyDelete
  14. http://www.oopsconsultancy.com/software/xmltask/

    ReplyDelete
  15. Hello Suman,

    Thanks for this quick tuto, it was very useful. However I'd like to do something and it looks liked I cannot find any clue by now. Here is the problem

    Let's say we add an id property to the Student nodes of you second example ( eg: ). Each student has a different id value.
    How can I do to display the name and other info of the Student with the id 1.

    Thanks,

    Laurent

    ReplyDelete
  16. Thanks for the detailed blog, I have a query, my xml file (which I want to read from ANT) is bit complicated :






    I want to read this xml file from ant and the output should be :
    C:\direc\data\abc.txt
    C:\direc\data\def.txt

    and I can't use any ant extensions like xmltask. Any help is greatly appreciated, thanks in advance.

    ReplyDelete
  17. hi,
    how to generate html reports bu using xml files? is it possible with ant?

    ReplyDelete
  18. Hi

    Suppose I have a xml like below














    Suppose in apache ant I want the name of the student of the who is from SGS college and whose id is "001"
    How can I get this ant without using any extra jar like antcontrib.jar and xmltask.jar.

    ReplyDelete
  19. Hi

    Suppose I have a xml like below














    Suppose in apache ant I want the name of the student of the who is from SGS college and whose id is "001"
    How can I get this ant without using any extra jar like antcontrib.jar and xmltask.jar.

    ReplyDelete
  20. Hi

    Suppose I have a xml like below














    Suppose in apache ant I want the name of the student of the who is from SGS college and whose id is "001"
    How can I get this ant without using any extra jar like antcontrib.jar and xmltask.jar.

    ReplyDelete

You can put your comments here (Either feedback or your Question related to blog)