本文介绍了在XmlSlurper的GPath中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法让XmlSlurper通过变量获得任意元素?
例如所以我可以做一些类似于
的输入文件:
< file>
< record name =某条记录/>
< record name =其他一些记录/>
< / file>
def xml = new XmlSlurper()。parse(inputFile)
String foo =record
return xml。{foo} .size()
我试过使用{}和$ {}和()如何转义那样的变量?或者是没有办法?
,是否可以使用闭包的结果作为参数?所以我可以做一些像
String foo = file.record
int numRecords = xml。{foo.find( /.\w+$/)}
解决方案
import groovy.xml。*
def xmltxt =< file>
< record name =某条记录/>
< record name =some other record/>
< / file>
def xml = new XmlSlurper()。parseText(xmltxt)
String foo =record
return xml。$ {foo}。size()
Is there a way to have XmlSlurper get arbitrary elements through a variable?e.g. so I can do something likeinput file:
<file>
<record name="some record" />
<record name="some other record" />
</file>
def xml = new XmlSlurper().parse(inputFile)
String foo = "record"
return xml.{foo}.size()
I've tried using {} and ${} and () how do I escape variables like that? Or isn't there a way?and is it possible to use results from closures as the arguments as well? So I could do something like
String foo = file.record
int numRecords = xml.{foo.find(/.\w+$/)}
解决方案
import groovy.xml.*
def xmltxt = """<file>
<record name="some record" />
<record name="some other record" />
</file>"""
def xml = new XmlSlurper().parseText(xmltxt)
String foo = "record"
return xml."${foo}".size()
这篇关于在XmlSlurper的GPath中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-10 00:05