jQuery从XML文档中提取和显示节点

时间:2014-09-05 04:40:44

标签: jquery xml

我使用jQuery ajax调用WS-Trust服务来检索SAML断言。我的服务响应包括完整的WS-Trust响应,我只需要从doc中间进行断言。

XML文档是:

    <ns3:RequestSecurityTokenResponse xmlns:ns1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:ns10="http://www.w3.org/2000/09/xmldsig#" xmlns:ns2="http://www.w3.org/2005/08/addressing" xmlns:ns3="http://docs.oasis-open.org/ws-sx/ws-trust/200512/" xmlns:ns4="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:ns5="http://docs.oasis-open.org/ws-sx/ws-trust/200802" xmlns:ns6="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:ns7="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:ns8="http://schemas.xmlsoap.org/ws/2005/02/sc" xmlns:ns9="http://schemas.xmlsoap.org/ws/2006/02/addressingidentity">
        <ns3:RequestedSecurityToken>
            <saml2:Assertion xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" xmlns:ns3="http://www.w3.org/2001/04/xmlenc#" xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" ID="SAML_4e367081-34ab-11e4-b48c-e062ecbc9f52" IssueInstant="2014-09-04T20:18:28.420-07:00" Version="2.0">
                <saml2:Issuer>pmdomain</saml2:Issuer>
                <saml2:Subject>
                    <saml2:NameID Format="urn:com:soa:saml:nameid-format:QualifiedName" NameQualifier="PolicyManager">Local LDAP\igoldsmith</saml2:NameID>
                    <saml2:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:sender-vouches" />
                </saml2:Subject>
                <saml2:Conditions NotOnOrAfter="2014-09-05T00:18:28.420-07:00" />
                <saml2:AuthnStatement AuthnInstant="2014-09-04T20:18:28.420-07:00">
                    <saml2:AuthnContext>
                        <saml2:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified</saml2:AuthnContextClassRef>
                    </saml2:AuthnContext>
                </saml2:AuthnStatement>
                <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    <ds:SignedInfo>
    <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
    <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
    <ds:Reference URI="#SAML_4e367081-34ab-11e4-b48c-e062ecbc9f52">
    <ds:Transforms>
    <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
    <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
    </ds:Transforms>
    <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
    <ds:DigestValue>/9WRlKPTL5N2SJOYe9pSw9Coy4I=</ds:DigestValue>
    </ds:Reference>
    </ds:SignedInfo>
    <ds:SignatureValue>
    Lgxq9PdlgSTU5R6JvhFBLbGv6PZUo64DhZvlovtg/sfTSZcljHKld9XQvE5cWT4Q2O0EVhxb332O
    XBipazlw8Tluvlf5s/k2m8L9LhtR2X/kdI+HooGEyHFBFLg4cX4nKP8Mbmc+bJegOtMlFSy/Zj5y
    gZr+bNwPk0xdniblmco=
    </ds:SignatureValue>
    <ds:KeyInfo>
    <ds:X509Data>
    <ds:X509Certificate>
    MIICATCCAWqgAwIBAgIIZ7i81vjsmF0wDQYJKoZIhvcNAQEFBQAwQzELMAkGA1UEBhMCVVMxCzAJ
    BgNVBAgTAkNBMQwwCgYDVQQKEwNTT0ExDDAKBgNVBAsTA1NPQTELMAkGA1UEAxMCcG0wHhcNMTQw
    NjE3MjA0ODIxWhcNMTkwNjE4MTM0NTAwWjBDMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExDDAK
    BgNVBAoTA1NPQTEMMAoGA1UECxMDU09BMQswCQYDVQQDEwJwbTCBnzANBgkqhkiG9w0BAQEFAAOB
    jQAwgYkCgYEAlyVusLGwuaFjL7nFdnvqbLVMq7T0wONpLTYDGMFwg5gEz+8gDzi7nZrtwxOe31V8
    gEz6u6KdZXfjBUNkp0JgDt8uBGWr55Rol6kYnxix5r/UwvU2gRCAl0TGwipyShqW+FmWyK/y/+R6
    5mtdD8FgmrmuBVwTGc2DxZxWJQOcqhMCAwEAATANBgkqhkiG9w0BAQUFAAOBgQAnoiHMEdGvVa57
    o+f+o6oknHsZDz8ipWG0vLzE+Pv3ordsVLKb2iG2o8DZAG7WuECirbXiLmZQ7cV+CqBiIqm82apZ
    Aw2pnD1uTBxaakf4KRkOct/Dk8MDr/2RyOUUQcxOnW3GeJV2q6YiIiGqAPN1/7Qf9cYSSQQC7/uV
    kFPf1g==
    </ds:X509Certificate>
    </ds:X509Data>
    </ds:KeyInfo>
    </ds:Signature>
            </saml2:Assertion>
        </ns3:RequestedSecurityToken>
        <ns3:TokenType>http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0</ns3:TokenType>
        <ns3:RequestedAttachedReference>
            <ns4:SecurityTokenReference>
                <ns4:Reference URI="#SAML_4e367081-34ab-11e4-b48c-e062ecbc9f52" ValueType="http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0" />
            </ns4:SecurityTokenReference>
        </ns3:RequestedAttachedReference>
        <ns3:Lifetime>
            <ns1:Created>2014-09-05T03:18:28.450Z</ns1:Created>
            <ns1:Expires>2014-09-05T07:18:28.420Z</ns1:Expires>
        </ns3:Lifetime>
    </ns3:RequestSecurityTokenResponse>

我有两个问题:

1 - 将saml2:Assertion节点提取到新的XML对象中的最佳方法是什么?

2 - 将此对象转换为字符串以便在我的页面正文中显示的最佳方法是什么?

感谢。

1 个答案:

答案 0 :(得分:0)

修改,更新

TODO:在标记

处保留原始文档大写字母

要呈现代码的内容,可以使用saml2\\:assertion outerHTML元素的saml2:Assertions元素xml

尝试

V2

HTML

<textarea></textarea>

JS

// `ajax` method , options
$.ajax()
// `ajax` `.done()` , `success` callback
.done(function(data, status, jqxhr) {
       var xml = $(data.documentElement);
       var assertion = xml
       .children("ns3\\:requestedsecuritytoken")
       .children("saml2\\:assertion");
       // raw xml , 
       // TODO: retain original document   
       // uppercase letters at tag 
       $("textarea").val(assertion[0].outerHTML);
   });

jsfiddle http://jsfiddle.net/guest271314/sqz12ma2/