被groovy彻底征服:
先瞅瞅人家groovy是怎样解析xml的:
plan.xml
<plan> <week capacity="8"> <task done="2" total="2" title="read XML chapter" /> <task done="3" total="3" title="try some reporting" /> <task done="1" total="2" title="use in current project" /> </week> <week capacity="8"> <task done="0" total="1" title="re-read DB chapter" /> <task done="0" total="3" title="use DB/XML combination" /> </week> </plan>
groovy解析代码如下:
println "begin"
def plan = new XmlParser().parse(new File('plan.xml'))
assert 'plan' == plan.name()
assert 'week' == plan.week[0].name()
assert 'task' == plan.week[0].task[0].name()
assert 'read XML chapter' == plan.week[0].task[0].'@title'
本来还打算在groovy 提供的xml API基础上搞一个DSL给用户用, 现在看来,完全没必要。
声明一下, 学习groovy不是跟风, 而是有需求。
groovy简洁的语法, 与java的无缝互调, 与spring的集成与groovy代码的热部署,想不用你都不行。