如何在使用XMLSlurper遍历文件时修改XML节点值 [英] How to modify XML node value, when iterating through the file with XMLSlurper

查看:744
本文介绍了如何在使用XMLSlurper遍历文件时修改XML节点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  def root = new XmlSlurper()。parseText(xml) 

root.service.each {service - >
service.receiver.endpoint.each {endpoint - >
println\t\tEndpoint:$ {endpoint.text()}
}
}

我想先读取endpoint.text(),然后修改节点的值,以便文件包含一个新值。
我正在阅读节点属性,因为它是。但是,我以后怎么写呢?



我看了这个问题,并了解如何写入现有文件。但是我问的是,当我已经遍历文件来读取它的一些内容时,它可以做得更加优雅吗?我希望有一个更有效的方式,适合我迭代文件的方式。



所以我希望有一种方法可以做到这一点:

p>

  def root = new XmlSlurper()。parseText(xml)

root.service.each {service - >
service.receiver.endpoint.each {endpoint - >
println\t\tEndpoint:$ {endpoint.text()}
// ****写入节点****
}
}

希望它有道理。



另外,这里有一些XML示例:

 < project命名= '普通' > 
< service name ='name'pattern ='something'isReliable ='maybe'>
< receiver name ='name'isUsingTwoWaySsl ='maybe'isWsRmDisabled ='maybe'
targetedByTransformation ='maybe'>
< endpoint name ='local_tst01'> URL< / endpoint>
< endpoint name ='local_tst02'> URL< / endpoint>
< endpoint name ='local_tst03'> URL< / endpoint>
< environment name ='dev'default ='local_dev'/>
< environment name ='tst01'default ='test'/>
< environment name ='tst02'default ='local_tst02'/>
< / receiver>
< operation name ='name'>
<发件人>发件人< /发件人>
< attribute name ='operation'type ='String'> name< / attribute>
< / operation>
< / service>
< / project>


解决方案

不知道为什么 replaceBody 方法受到保护,但它可以工作:

  def root = new XmlSlurper()。 parseText(
'''< root>
< service>
< receiver>
< endpoint> 123< / endpoint>
< endpoint> 456< / endpoint>
< / receiver>
< / service>
< / root>''')

root.service.each {服务 - >
service.receiver.endpoint.each {endpoint - >
endpoint.replaceBody(**+ endpoint.text())
}
}

println groovy.xml.XmlUtil.serialize(root)


I am currently iterating through an XML file like this:

def root = new XmlSlurper().parseText(xml)

root.service.each { service ->
  service.receiver.endpoint.each { endpoint ->
    println "\t\tEndpoint: ${endpoint.text()}"
    }
}

I want to first read the endpoint.text() , and afterwards modify the value of the node, so the file contains a new value. I am reading the node attributes fine, as it is. But how do I write to it afterwards?

I looked at this question, and understand how to write to an existing file. But what I am asking is, can it be done in a more elegant matter, when I am already iterating through the file to read some of it's content? I am hoping there is a more efficient way that suits the way I am iteraring through the file.

So am hoping there is a way to do something like this:

def root = new XmlSlurper().parseText(xml)

root.service.each { service ->
  service.receiver.endpoint.each { endpoint ->
    println "\t\tEndpoint: ${endpoint.text()}"
    // ****WRITE TO NODE****
    }
}

hope it makes sense.

Also, here is some example XML:

<project name='Common'>
  <service name='name' pattern='something' isReliable='maybe'>
    <receiver name='name' isUsingTwoWaySsl='maybe' isWsRmDisabled='maybe' 
       targetedByTransformation='maybe'>
      <endpoint name='local_tst01'>URL</endpoint>
      <endpoint name='local_tst02'>URL</endpoint>
      <endpoint name='local_tst03'>URL</endpoint>
      <environment name='dev' default='local_dev' />
      <environment name='tst01' default='test' />
      <environment name='tst02' default='local_tst02' />
    </receiver>
    <operation name='name'>
      <sender>sender</sender>
      <attribute name='operation' type='String'>name</attribute>
    </operation>
  </service>
</project>

解决方案

don't know why replaceBody method is protected but it works:

def root = new XmlSlurper().parseText(
'''<root>
    <service>
        <receiver>
            <endpoint>123</endpoint>
            <endpoint>456</endpoint>
        </receiver>
    </service>
</root>''')

root.service.each { service ->
    service.receiver.endpoint.each { endpoint ->
        endpoint.replaceBody("**"+endpoint.text())
    }
}

println groovy.xml.XmlUtil.serialize( root )

这篇关于如何在使用XMLSlurper遍历文件时修改XML节点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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