XmlSlurper:如何更改动态节点的文本 [英] XmlSlurper: How to change the text of a dynamic node

查看:104
本文介绍了XmlSlurper:如何更改动态节点的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<Messdaten>
  <EL_NR>NAYP99</EL_NR>
  <EL_NR_Original/>
  <Erfassungsdatum>2012-12-12 11:58:54.000</Erfassungsdatum>
  <Massnahme>Lot_Hold</Massnahme>
  <Anzahl_x0020_R>50</Anzahl_x0020_R>
  <FEHLER>OK</FEHLER>
  <SEQ>72</SEQ>
</Messdaten>

这是我的XML,我想更改节点'Anzahl_x0020_R'的值。问题是,在运行时我不知道确切的名称。我只知道要改变的节点将以'Anzahl'开头。所以我这样做:

That is my XML and i want to change the value of node 'Anzahl_x0020_R'. The problem is, at runtime i don't know the exact name. I only know that the node to change will start with 'Anzahl'. So i did this:

messdatenXML.childNodes().each { merkmal ->
    if (merkmal.name.contains('Anzahl')) {
        messdatenXML.merkmal = "my_new_value";
    }
}

事情是,它不起作用,价值将保持不变。如果我这样做:

The thing is, it doesn't work, the value will stay the same. If i do:

messdatenXML.Anzahl_x0020_R = "my_new_value"

它可以工作,但正如我之前所说的,我不知道运行时的确切名称。

it will work but as i said before, i don't know the exact name at runtime.

推荐答案

我相信你需要像这样使用 replaceBody

I believe you need to use replaceBody like so:

messdatenXML.'**'.findAll { it.name().startsWith 'Anzahl' }.each { node ->
  node.replaceBody 'my new value'
}
println groovy.xml.XmlUtil.serialize( messdatenXML )

这篇关于XmlSlurper:如何更改动态节点的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆