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
Good Post sir
ReplyDeleteHi Binod. Really awesome work. Only yesterday I put my doubt and today you have post this article on XMLTASK on my request.
ReplyDeleteThank you very much.
Jack.
How to read xml node attribute value?
ReplyDeleteis there any way to change a XML node using in build ANT tasks?
ReplyDeleteHi,
ReplyDeleteCan 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