如何获取 XML 和 XML 路径的属性值?

How to obtain Attribute Values of an XML with XMLPath?

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

我可以通过以下方式获取 md:EntityDescriptor 的实体 ID:entityId=metadataXml.get("md:EntityDescriptor.@entityID")

但是对于 Location 属性,我正在努力获得

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

我得到的输出是 [],没有别的。

给你,在线评论:

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