0

我有一个 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,获取属性的 XMLPath 表达式是Location什么md:SingleLogoutService

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

但是对于我想要得到的 Location 属性

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

我得到的输出是[],仅此而已。

4

1 回答 1

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

于 2017-07-13T23:48:57.713 回答