在下面的代码中,我正在尝试更新其中一个标记值。我的问题是我需要更新哪个标记值,我只知道在运行时。 下面的代码无法更新标记值,即使我能够通过此代码获取标记值。
def temp1="""
<TradeRecord>
<TradeId>1000</TradeId>
<SecurityId>10382456</SecurityId>
<TradeType>SELLL</TradeType>
<TradeDate>2014-03-21</TradeDate>
<Broker>100</Broker>
<Exchange>1</Exchange>
<Status>INIT</Status>
<Quantity>12765</Quantity>
<ApprovedBy />
</TradeRecord>
"""
def records = new XmlParser().parseText(temp1)
childRecords = records.children()
j = childRecords.size()
def tagname = 'Quantity'
for (int i=0; i<j; i++){
if (childRecords[i].name() == tagname) {
log.info childRecords[i].text()
childRecords[i].text() = "9999"
log.info childRecords[i].text()
}
}
答案 0 :(得分:0)
你不能分配给text()
- getter,但有value
,它有一个setter; e.g:
childRecords[i].value = "9999"
但这也可以做得更加时髦:
def temp1="""
<TradeRecord>
<TradeId>1000</TradeId>
<SecurityId>10382456</SecurityId>
<TradeType>SELLL</TradeType>
<TradeDate>2014-03-21</TradeDate>
<Broker>100</Broker>
<Exchange>1</Exchange>
<Status>INIT</Status>
<Quantity>12765</Quantity>
<ApprovedBy />
</TradeRecord>
"""
def records = new XmlParser().parseText(temp1)
final tagname = 'Quantity'
records."$tagname".each{
it.value = '9999'
}
println groovy.xml.XmlUtil.serialize(records)