在XmlSlurper的GPath中使用变量
发布时间:2020-12-14 16:32:50 所属栏目:大数据 来源:网络整理
导读:有没有办法让XmlSlurper通过变量获得任意元素? 例如所以我可以做点什么 输入文件: file record name="some record" / record name="some other record" //filedef xml = new XmlSlurper().parse(inputFile)String foo = "record"return xml.{foo}.size()
有没有办法让XmlSlurper通过变量获得任意元素?
例如所以我可以做点什么 输入文件: <file> <record name="some record" /> <record name="some other record" /> </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="some record" /> <record name="some other record" /> </file>""" def xml = new XmlSlurper().parseText(xmltxt) String foo = "record" return xml."${foo}".size() (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |