grails - 使用Groovy读取输入xml和构造输出xml时出错

标签 grails groovy groovy-console

我试图使用groovy从输入xml提取值并构造输出xml。我遇到了错误。我举了一个小例子来验证逻辑。

 def xml1 = """
 <company type="bentley">
     <account>
      <dept1>IT</dept1>
      <dept2>Admin</dept2>
      <dept3>Security</dept3>
    </account>
</company>""" ;

def xml2 = """
<company type="NG">
    <account>
      <dept1></dept1>
      <dept2></dept2>
      <dept3></dept3>
     </account>
</company>""";

def rootnode1= new XmlParser().parseText(xml1);
def rootnode2= new XmlParser().parseText(xml2);

rootnode1.account.each {
rootnode2.account.dept1[0].value = it.dept1[0].text();
rootnode2.account.dept2[0].value = it.dept2[0].text();
}

错误:java.lang.IllegalArgumentException:参数类型不匹配

谢谢,
文卡特

最佳答案

看来您需要为 each 函数提供一个计数器。尝试如下操作:

rootnode1.account.each { n ->
rootnode2.account.dept1[0].value = n.dept1[0].text();
rootnode2.account.dept2[0].value = n.dept2[0].text();
}

更好的是,如果部门数量未知,则可以将.each()移至部门级别。

关于grails - 使用Groovy读取输入xml和构造输出xml时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46691513/

相关文章:

grails - 将 session 详细信息添加到 Controller 日志方法

unit-testing - Grails:单元测试中的NPE

java - ORA-28040 : No matching authentication protocol exception when using groovy. 带有 oracle 12c 的 sql 包

grails - 您如何在Grails/Spock测试中而不是在org.grails.plugins.testing.GrailsMockHttpServletResponse@62c0fcae中打印响应

grails - "grails console"和 "grails"之间的差异,然后是 "console"

unit-testing - 我如何模拟grails domain.refresh()

hibernate - Grails Db反向工程生成可序列化而不是时间戳

hibernate - Grails Domain Binder.getMapping(Domain)对于Grails-3.2.4中的非持久性域返回null

grails - 仅显示最后10个字符

Groovy 中的 Unix FIND 命令