gpt4 book ai didi

XmlSlurper.appendNode 不改变大小

转载 作者:数据小太阳 更新时间:2023-10-29 02:58:57 26 4
gpt4 key购买 nike

我使用 XmlSlurper 处理 XML。它工作正常,直到我更新它。 appendNode 不反射(reflect)大小。

结构更新后如何使用 XmlSlurper?

XML 定义:

def CAR_RECORDS = '''
<records>
<car name='HSV Maloo' make='Holden' year='2006'>
<country>Australia</country>
<record type='speed'>Production Pickup Truck with speed of 271kph</record>
</car>
<car name='P50' make='Peel' year='1962'>
<country>Isle of Man</country>
<record type='size'>Smallest Street-Legal Car at 99cm wide and 59 kg in weight</record>
</car>
<car name='Royale' make='Bugatti' year='1931'>
<country>France</country>
<record type='price'>Most Valuable Car at $15 million</record>
</car>
</records>
'''

代码女巫失败:

def records = new XmlSlurper().parseText(CAR_RECORDS)
records.appendNode( { car(make:'BMW') } )
assert 4 == records.car.size() //fails!!! size == 3

打印出包含宝马车的XML

def xmlOut = new groovy.xml.StreamingMarkupBuilder()    
def temp = xmlOut.bind{
mkp.yield records
}
​println temp​

最佳答案

您可以使用 XmlSlurper 再次读取新创建的结构。

...

records = new XmlSlurper().parseText(temp as String)
assert 4 == records.car.size()

关于XmlSlurper.appendNode 不改变大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6255287/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com