1
我遇到問題以編程方式使用replaceNode時,一個簡單的示例工作正常。Groovy XmlSlurper replaceNode無法與找到的節點一起工作
基於以前的問題,此代碼可以工作,但換出標記爲SWAP的行並將其中斷。
任何人都可以告訴我如何使這個工作與從查找{}調用返回的節點?
package com.az.util.test
import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil
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)
def allRecords = records.car
assert 3 == allRecords.size()
def firstRecord = records.car[0]
assert 'car' == firstRecord.name()
println 'country before: ' + firstRecord.'country'.text()
def secondRecord = records.car[1]
// SWAP
//firstRecord.'country'.replaceNode {secondRecord.country}
firstRecord.'country'.replaceNode {country("Isle of Man")}
records = new XmlSlurper().parseText(XmlUtil.serialize(new StreamingMarkupBuilder().bind {
mkp.yield records
}))
println 'country after: ' + records.car.country.find{ true }.text()
println XmlUtil.serialize(new StreamingMarkupBuilder().bind {
mkp.yield records
})