首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XMLSlurper:在使用XmlSlurper时没有找到任何节点(false,false)

XMLSlurper:在使用XmlSlurper时没有找到任何节点(false,false)
EN

Stack Overflow用户
提问于 2017-02-02 12:34:09
回答 1查看 399关注 0票数 0

当我创建XMLSlurper时,没有验证和命名空间意识:

代码语言:javascript
复制
new XmlSlurper(false, false)

随后,我找不到node.depthFirst().findAll()的任何节点。

以下代码说明了我的问题:

代码语言:javascript
复制
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

是已知的窃听器还是我漏掉了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-15 08:32:42

当XmlSlurper的属性namespaceAware设置为false时,我们需要将命名空间前缀作为元素名称的一部分,即当我们查找元素时:

代码语言:javascript
复制
(node.name() == "xt:replace")

XmlSlurper的这种行为在我看来并不正确,但它的工作方式是正确的。双冒号(:)不能是XML元素名称的一部分,而是保留给命名空间使用。我的结论是,我将在将来使用namespaceAware set。

谢谢你的帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42002433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档