当我创建XMLSlurper时,没有验证和命名空间意识:
new XmlSlurper(false, false)随后,我找不到node.depthFirst().findAll()的任何节点。
以下代码说明了我的问题:
def xml = '''<?xml version="1.0" encoding="UTF-8"?>
<cus:Customizations xmlns:cus="http://www.bea.com/wli/config/customizations" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xt="http://www.bea.com/wli/config/xmltypes">
<cus:customization xsi:type="cus:EnvValueActionsCustomizationType">
<cus:description/>
<cus:actions>
<xt:replace>
<xt:envValueType>Service URI</xt:envValueType>
<xt:location>0</xt:location>
<xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">http://myUrl.com</xt:value>
</xt:replace>
</cus:actions>
</cus:customization>
</cus:Customizations>'''
/* The following code finds no nodes. */
def envelope1 = new XmlSlurper(false, false).parseText(xml)
def replaces1 = envelope1.depthFirst().findAll { node ->
(node.name() == "replace")
}
assert replaces1.size() == 0
/* The following code finds one node (works as I expect). */
def envelope2 = new XmlSlurper().parseText(xml)
def replaces2 = envelope2.depthFirst().findAll { node ->
(node.name() == "replace")
}
assert replaces2.size() == 1是已知的窃听器还是我漏掉了什么?
发布于 2017-02-15 08:32:42
当XmlSlurper的属性namespaceAware设置为false时,我们需要将命名空间前缀作为元素名称的一部分,即当我们查找元素时:
(node.name() == "xt:replace")XmlSlurper的这种行为在我看来并不正确,但它的工作方式是正确的。双冒号(:)不能是XML元素名称的一部分,而是保留给命名空间使用。我的结论是,我将在将来使用namespaceAware set。
谢谢你的帮助。
https://stackoverflow.com/questions/42002433
复制相似问题