简体   繁体   English

XmlSlurper 获取节点值说明

[英]XmlSlurper get node value clarification

XML file 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>

I want to read the value of the particular tag, I'm using XMLSlurper, below is my code我想读取特定标签的值,我正在使用 XMLSlurper,下面是我的代码

String sample ='to'
def person = new XmlSlurper().parse(new File("C:\\Desktop\\note.xml"))
println  person.to

for the above getting the answer = Tove .对于上面得到的答案 = Tove

But when I pass the tag name as string I'm not getting the value但是当我将标签名称作为字符串传递时,我没有得到值

String sample ='to'
def person = new XmlSlurper().parse(new File("C:\\Desktop\\note.xml"))
println  person.sample

getting empty string得到空字符串

Let me know how can I handle this?让我知道我该如何处理?

Given your example you should use use your variable like this and let it be interpreted as a GString:鉴于您的示例,您应该像这样使用您的变量,并将其解释为 GString:

String sample ='to'
def person = new XmlSlurper().parse(new File("I:/Work/test.xml"))
println  person."${sample}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM