Sunday, April 4, 2010

XML Handling in Groovy, XML with Groovy

Info.xml

<?xml version="1.0" ?>
<customers>
<corporate>
<customer name="Nik Kumar Suman" company="TecnoTree" />
<customer name="Pramod Kumar Modi" company="efi" />
<customer name="Mika" company="TecnoTree" />
</corporate>

<consumer>
<customer name="Nik" />
<customer name="Pramod" />
</consumer>

</customers>

ReadXML.gy

def customers = new XmlSlurper().parse(new File('Info.xml'))
for (customer in customers.corporate.customer){
println "${customer.@name} works for ${customer.@company}"
}

output:

D:\Nik\Groovy_Example>groovy ReadXML.gy
Nik Kumar Suman works for TecnoTree
Pramod Kumar Modi works for efi
Mika works for TecnoTree
D:\Nik\Groovy_Example>

No comments:

Post a Comment