Monday, June 22, 2009

How to read an XML file and extract values for the attributes using ANT script, XML Manipulation using XMLTask, XMLTASK example, Parse XML file in ANT

Parse XML file in ANT using XMLTASK

1. Write one ant script (xmlRead.xml)
2. Write one xml fiel (tests2.xml)
3. Download jar file (xmltask-v1.15.1.jar) from here and put in lib folder (c:\xmltask\lib)

1. xmlRead.xml (c:\xmltask\src)

<?xml version="1.0" encoding="UTF-8"?>
<project name="ReadXML" default="readXML">

<path id="build.classpath"><fileset dir="lib">
<include name="xmltask-v1.15.1.jar" />
</fileset>
</path>

<taskdef name="xmltask"classname="com.oopsconsultancy.xmltask.ant.XmlTask"
classpathref="build.classpath" />

<target name="readXML">
<xmltask source="tests2.xml">

<call path="/CHECK/TESTCASES/TESTCASE">
<param name="val" path="text()" />

<actions><echo>Test Case Details = @{val}</echo></actions>

</call>
</xmltask>

</target>
</project>

2. tests2.xml (c:\xmltask\src)

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

<CHECK>

<TESTCASES>
<TESTCASE>ReceiveImageTest</TESTCASE>
<TESTCASE>ValidateImageTest</TESTCASE>
<TESTCASE>PublishImageTest</TESTCASE>
</TESTCASES>

<TESTCASES>
<TESTCASE>ReceiveImageTest2</TESTCASE>
<TESTCASE>ValidateImageTest2</TESTCASE>
<TESTCASE>PublishImageTest2</TESTCASE>
</TESTCASES>

</CHECK>

After run the build.xml, output would be

Buildfile: C:\xmltask\src\\xmlRead.xml
readXML:
[echo] Test Case Details = ReceiveImageTest
[echo] Test Case Details = ValidateImageTest
[echo] Test Case Details = PublishImageTest
[echo] Test Case Details = ReceiveImageTest2
[echo] Test Case Details = ValidateImageTest2
[echo] Test Case Details = PublishImageTest2
BUILD SUCCESSFULTotal time: 562 milliseconds

5 comments:

  1. Hi Binod. Really awesome work. Only yesterday I put my doubt and today you have post this article on XMLTASK on my request.

    Thank you very much.
    Jack.

    ReplyDelete
  2. How to read xml node attribute value?

    ReplyDelete
  3. is there any way to change a XML node using in build ANT tasks?

    ReplyDelete
  4. Hi,

    Can you help with this:



    1
    1
    1
    1
    1
    Warning


    2
    2
    2
    2
    2
    Warning



    I need to print:
    1
    1
    1
    1
    2
    2
    2
    2

    So I need to loop at parent node level and read all the child elements

    ReplyDelete

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