如何使用groovy获取XML属性的值

如何使用groovy获取XML属性的值,xml,groovy,xml-parsing,xmlslurper,Xml,Groovy,Xml Parsing,Xmlslurper,我试图在Groovy中获取CanOfferProductResponse内xmlns属性的值 下面是我的XML- <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SOAP-ENV:B

我试图在Groovy中获取CanOfferProductResponsexmlns属性的值

下面是我的XML-

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SOAP-ENV:Body><CanOfferProductResponse xmlns="urn:iQQ:API:22:iQQMessages.xsd"/></SOAP-ENV:Body></SOAP-ENV:Envelope>
//预期输出=urn:iQQ:API:22:iQQMessages.xsd(位于标记内)


我是XML新手,请帮助我

使用XML名称空间可能会使事情复杂化。如果您知道XML代码段使用的是如图所示的确切名称空间前缀,那么可以在XmlSlurper中禁用名称空间感知,并使用“prefix:elementName”作为引用元素的前缀

def xml = '''<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SOAP-ENV:Body>
        <CanOfferProductResponse xmlns="urn:iQQ:API:22:iQQMessages.xsd"/>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
'''

// the second constructor argument controls namespace awareness
def env = new XmlSlurper(false, false).parseText(xml)
def namespace = env.'SOAP-ENV:Body'.CanOfferProductResponse.@xmlns
assert namespace == 'urn:iQQ:API:22:iQQMessages.xsd'
但是由于名称空间是继承的,这种方法意味着
lookupNamespace
方法仍然会返回“urn:iQQ:API:22:iqqqMessages.xsd”,即使
CanOfferProductResponse
元素上没有
xmlns
属性,例如

def xml = '''<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                   xmlns="urn:iQQ:API:22:iQQMessages.xsd">
    <SOAP-ENV:Body>
        <CanOfferProductResponse />
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
'''

def env = new XmlSlurper().parseText(xml)
def namespace = env.Body.CanOfferProductResponse.lookupNamespace('')
assert namespace == 'urn:iQQ:API:22:iQQMessages.xsd'
defxml=''
'''
def env=new XmlSlurper().parseText(xml)
def namespace=env.Body.CanOfferProductResponse.lookupNamespace(“”)
断言名称空间=='urn:iQQ:API:22:iQQMessages.xsd'

(这个示例是用Groovy 2.5执行的)

使用XML名称空间可能会使事情变得复杂。如果您知道XML代码段使用的是如图所示的确切名称空间前缀,那么可以在XmlSlurper中禁用名称空间感知,并使用“prefix:elementName”作为引用元素的前缀

def xml = '''<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SOAP-ENV:Body>
        <CanOfferProductResponse xmlns="urn:iQQ:API:22:iQQMessages.xsd"/>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
'''

// the second constructor argument controls namespace awareness
def env = new XmlSlurper(false, false).parseText(xml)
def namespace = env.'SOAP-ENV:Body'.CanOfferProductResponse.@xmlns
assert namespace == 'urn:iQQ:API:22:iQQMessages.xsd'
但是由于名称空间是继承的,这种方法意味着
lookupNamespace
方法仍然会返回“urn:iQQ:API:22:iqqqMessages.xsd”,即使
CanOfferProductResponse
元素上没有
xmlns
属性,例如

def xml = '''<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                   xmlns="urn:iQQ:API:22:iQQMessages.xsd">
    <SOAP-ENV:Body>
        <CanOfferProductResponse />
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
'''

def env = new XmlSlurper().parseText(xml)
def namespace = env.Body.CanOfferProductResponse.lookupNamespace('')
assert namespace == 'urn:iQQ:API:22:iQQMessages.xsd'
defxml=''
'''
def env=new XmlSlurper().parseText(xml)
def namespace=env.Body.CanOfferProductResponse.lookupNamespace(“”)
断言名称空间=='urn:iQQ:API:22:iQQMessages.xsd'
(此示例使用Groovy 2.5执行)