假设现有customer.xml 文件内容如下:

 <?xml version="1.0" ?>
<customers>
<corporate>
<customer name="Bill Gates" companty="Microsoft"/>
<customer name="Steve Jobs" companty="Apple"/>
<customer name="Jonathan Schwartz" companty="Sun"/>
</corporate>
<consumer>
<customer name="John Doe"/>
<customer name="Jane Doe"/>
</consumer>
</customers>

使用Grovvy语言输出所有的“corporate”客户的名称和公司:

package com.andy.test

/**
* Created by Andy on 16/5/8.
*/ def customers=new XmlSlurper().parse(new File('customer.xml'))
for(customer in customers.corporate.customer){
println "${customer.@name} works for ${customer.@companty}"
}

输出结果为:

Bill works for Microsoft

Steve Jobs works for Apple

Jonathan Schwarts works for Sun

05-11 01:47