XML文件
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>我想读取特定标记的值,我使用的是XMLSlurper,下面是我的代码
String sample ='to'
def person = new XmlSlurper().parse(new File("C:\\Desktop\\note.xml"))
println person.to对于上面的问题,得到的答案是= Tove。
但是,当我将标记名作为字符串传递时,我得不到值
String sample ='to'
def person = new XmlSlurper().parse(new File("C:\\Desktop\\note.xml"))
println person.sample获取空字符串
让我知道我怎么处理这个问题?
发布于 2019-10-15 19:40:37
给定您的示例,您应该像这样使用您的变量,并将其解释为GString:
String sample ='to'
def person = new XmlSlurper().parse(new File("I:/Work/test.xml"))
println person."${sample}"https://stackoverflow.com/questions/58392784
复制相似问题