使用 groovy 删除具有命名空间的特定 xml 节点
Remove a specific xml node with namespace using groovy
正在尝试使用 groovy
remove xml
个节点。这里要删除 requiredByDate
元素,该元素多次出现并且 namcespace 带有前缀。
查看了网上的许多示例,以及 stackover。其中一些很接近。如果该 xml 元素没有命名空间,则获取所需的输出。
问题是 xml 元素有命名空间 并且无法实现预期的结果。
这是我正在尝试的 groovy 脚本:
import groovy.xml.*
def x='''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com="http://www.example/test/commontypes" xmlns:ord="http://www.example/test/orderservice" xmlns:ord1="http://www.example/test/order">
<soapenv:Header/>
<soapenv:Body>
<ord:orderRequest>
<ord1:orderRef>${#TestCase#ORDERREF}</ord1:orderRef>
<ord1:header>
<ord1:description>user test</ord1:description>
<ord1:customerID></ord1:customerID>
<ord1:requiredByDate>2010-02-02T12:00:00-07:00</ord1:requiredByDate>
</ord1:header>
<ord1:line>
<ord1:lineNumber>1</ord1:lineNumber>
<ord1:actionMode>mode1</ord1:actionMode>
<ord1:requiredByDate>2010-02-02T12:00:00-07:00</ord1:requiredByDate>
</ord1:line>
<ord1:line>
<ord1:lineNumber>2</ord1:lineNumber>
<ord1:action>userAction</ord1:action>
<ord1:requiredByDate>2010-02-02T12:00:00-07:00</ord1:requiredByDate>
</ord1:line>
</ord:orderRequest>
</soapenv:Body>
</soapenv:Envelope>'''
def xml=new XmlParser().parseText(x)
def nodes = xml.'**'.findAll{ it.name() == 'requiredByDate' }
nodes.each{it.parent().remove(it)}
XmlUtil.serialize(xml).toString()
输出与输入相同,即不删除 requiredByDate
个元素(在 xml 中出现 3 次)
如果我使用名称空间进行硬编码,即 'ord1:requiredByDate'
,那么就会出现所需的输出。在此处引用 xml.'**'.findAll{ it.name() == 'ord1:requiredByDate' }
。
但是,我不知道运行时 xml 中的前缀是什么。因此,不能使用上面 findAll
中的硬编码前缀。
具有命名空间 return 的节点 QName
用于他的 name()
方法。您可以使用 QName.getLocalPart()
方法访问没有名称空间的节点的 "local" 名称
试试这个:
def nodes = xml.'**'.findAll{ it.name().localPart == 'requiredByDate' }
或 Node 的 javadoc:
Typically the name is a String and a value is either a String or a
List of other Nodes, though the types are extensible to provide a
flexible structure, e.g. you could use a QName as the name which
includes a namespace URI and a local name.
正在尝试使用 groovy
remove xml
个节点。这里要删除 requiredByDate
元素,该元素多次出现并且 namcespace 带有前缀。
查看了网上的许多示例,以及 stackover。其中一些很接近。如果该 xml 元素没有命名空间,则获取所需的输出。
问题是 xml 元素有命名空间 并且无法实现预期的结果。
这是我正在尝试的 groovy 脚本:
import groovy.xml.*
def x='''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com="http://www.example/test/commontypes" xmlns:ord="http://www.example/test/orderservice" xmlns:ord1="http://www.example/test/order">
<soapenv:Header/>
<soapenv:Body>
<ord:orderRequest>
<ord1:orderRef>${#TestCase#ORDERREF}</ord1:orderRef>
<ord1:header>
<ord1:description>user test</ord1:description>
<ord1:customerID></ord1:customerID>
<ord1:requiredByDate>2010-02-02T12:00:00-07:00</ord1:requiredByDate>
</ord1:header>
<ord1:line>
<ord1:lineNumber>1</ord1:lineNumber>
<ord1:actionMode>mode1</ord1:actionMode>
<ord1:requiredByDate>2010-02-02T12:00:00-07:00</ord1:requiredByDate>
</ord1:line>
<ord1:line>
<ord1:lineNumber>2</ord1:lineNumber>
<ord1:action>userAction</ord1:action>
<ord1:requiredByDate>2010-02-02T12:00:00-07:00</ord1:requiredByDate>
</ord1:line>
</ord:orderRequest>
</soapenv:Body>
</soapenv:Envelope>'''
def xml=new XmlParser().parseText(x)
def nodes = xml.'**'.findAll{ it.name() == 'requiredByDate' }
nodes.each{it.parent().remove(it)}
XmlUtil.serialize(xml).toString()
输出与输入相同,即不删除 requiredByDate
个元素(在 xml 中出现 3 次)
如果我使用名称空间进行硬编码,即 'ord1:requiredByDate'
,那么就会出现所需的输出。在此处引用 xml.'**'.findAll{ it.name() == 'ord1:requiredByDate' }
。
但是,我不知道运行时 xml 中的前缀是什么。因此,不能使用上面 findAll
中的硬编码前缀。
具有命名空间 return 的节点 QName
用于他的 name()
方法。您可以使用 QName.getLocalPart()
试试这个:
def nodes = xml.'**'.findAll{ it.name().localPart == 'requiredByDate' }
或 Node 的 javadoc:
Typically the name is a String and a value is either a String or a List of other Nodes, though the types are extensible to provide a flexible structure, e.g. you could use a QName as the name which includes a namespace URI and a local name.