waterye

GPath

GPath is a path expression language we've integrated into Groovy which is similar in aims and scope as XPath is to XML.

import groovy.util.*

def text 
= """
<languages>
    
<language id="1" name="Java">
        
<attention>Spring</attention>
        
<attention>Hibernate</attention>
        
<attention>MyFaces</attention>
        
<attention>Groovy</attention>
        
<attention>Seam</attention>
        
<attention>XFire</attention>
        
<attention>Liferay</attention>
    
</language>
    
<language id="2" name="Python">
        
<attention></attention>
    
</language>
</languages>
"""

def node 
= new XmlParser().parseText(text)

def languages 
= node.children()
for (language in languages) {
    println language[
'@name']
}


def attentions 
= node.language.find { it['@id'== '1' }.attention
for (attention in attentions) {
    println attention.parent()[
'@name'+ ",  " + attention.text()
}

参考: http://docs.codehaus.org/display/GROOVY/GPath

posted on 2005-12-14 12:46 waterye 阅读(443) 评论(0)  编辑  收藏 所属分类: groovy


只有注册用户登录后才能发表评论。


网站导航: