如何使用XMLPath获取XML的属性值?

时间:2017-07-13 18:05:25

标签: xml groovy gpath

我有一个XMLPath对象,我希望获取某些节点的属性名称。例如,

<?xml version="1.0" encoding="UTF-8"?>
<md:EntityDescriptor entityID="https://scspr0269974001.c4.com/saml/metadata" ID="https___scspr0269974001.c2_saml_metadata" xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata">
  <md:SPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol" WantAssertionsSigned="true" AuthnRequestsSigned="true">
    <md:SingleLogoutService Location="https://scspr0269974001.company2.com/saml/SingleLogout" Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"/>
       <md:NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress</md:NameIDFormat>
       <md:NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:transient</md:NameIDFormat>
       <md:NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:persistent</md:NameIDFormat>
       <md:NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified</md:NameIDFormat>
       <md:NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName</md:NameIDFormat>
       <md:AssertionConsumerService Location="https://scspr0269974001.company1.com/saml/SSO" Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" isDefault="true" index="0"/>
    <md:AssertionConsumerService Location="https://scspr0269974001.company2.com/saml/SSO" Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Artifact" index="1"/>
  </md:SPSSODescriptor>
</md:EntityDescriptor>

对于此XML,获取Location的属性md:SingleLogoutService的XMLPath表达式是什么?

我可以使用以下代码获取md:EntityDescriptor的entityID:entityId=metadataXml.get("md:EntityDescriptor.@entityID")

但是对于Location属性,我试图得到它

logoutURL=metadataXml.get("'md:EntityDescriptor'.'md:SPSSODescriptor'.'md:SingleLogoutService'.@Location")

我获得的输出是[]而没有别的。

1 个答案:

答案 0 :(得分:1)

在这里,内联评论:

def xml = """<?xml version="1.0" encoding="UTF-8"?>
<md:EntityDescriptor entityID="https://scspr0269974001.c4.com/saml/metadata" ID="https___scspr0269974001.c2_saml_metadata" xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata">
  <md:SPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol" WantAssertionsSigned="true" AuthnRequestsSigned="true">
    <md:SingleLogoutService Location="https://scspr0269974001.company2.com/saml/SingleLogout" Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"/>
       <md:NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress</md:NameIDFormat>
       <md:NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:transient</md:NameIDFormat>
       <md:NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:persistent</md:NameIDFormat>
       <md:NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified</md:NameIDFormat>
       <md:NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName</md:NameIDFormat>
       <md:AssertionConsumerService Location="https://scspr0269974001.company1.com/saml/SSO" Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" isDefault="true" index="0"/>
    <md:AssertionConsumerService Location="https://scspr0269974001.company2.com/saml/SSO" Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Artifact" index="1"/>
  </md:SPSSODescriptor>
</md:EntityDescriptor>"""
//Parse the xml with XmlSlurper
def pxml = new XmlSlurper().parseText(xml)
//Extract the required attribute and print
println pxml.'**'.find{it.name() == 'SingleLogoutService'}.@Location.text()
//alternative to above line is that you can use below statement as well; both yield same result
println pxml.SPSSODescriptor.SingleLogoutService.@Location.text()

您可以在线快速尝试 Demo