Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groovy XmlSlurper replaceNode不适用于找到的节点_Groovy_Xmlslurper - Fatal编程技术网

Groovy XmlSlurper replaceNode不适用于找到的节点

Groovy XmlSlurper replaceNode不适用于找到的节点,groovy,xmlslurper,Groovy,Xmlslurper,当一个简单的示例运行良好时,我在以编程方式使用replaceNode时遇到问题 基于前面的一个问题,这段代码可以工作,但是调出标记为swap的行,它就会中断 有人能告诉我如何使用从find{}调用返回的节点进行此操作吗 package com.az.util.test import groovy.xml.StreamingMarkupBuilder import groovy.xml.XmlUtil def CAR_RECORDS = ''' <records>

当一个简单的示例运行良好时,我在以编程方式使用replaceNode时遇到问题

基于前面的一个问题,这段代码可以工作,但是调出标记为swap的行,它就会中断

有人能告诉我如何使用从find{}调用返回的节点进行此操作吗

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
  } )

将XmlSlurper与groovy结合使用,它与XPath完美结合:

def xmlDoc = """<parent>
    <child>
      <child1>
        <subchild1>sub1</subchild1>
      </child1>
    </child>
  <child>  
  <child2>
    <subchild2>sub2</subchild2>
  </child2>
  </child>  
</parent>"""


def sl = new XmlSlurper().parseText(xmlDoc)
def childsRecords = sl.child

childsRecords.child1.subchild1.each { println "$it" }