Tuesday, October 9, 2012

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

0 comments:

Post a Comment