代码之家  ›  专栏  ›  技术社区  ›  user68288

在Javascript中提取特定的XML属性

  •  0
  • user68288  · 技术社区  · 7 年前

    XML文档

    <samlp:Response ID="ffff"
                    Version="2.0"
                    IssueInstant="207Z"
                    Destination="htm"
                    Consent="urn:oasis:names:tc:SAML:2.0:consent:unspecified"
                    xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol">
      <Issuer xmlns="urn:oasis:names:tc:SAML:2.0:assertion">sites/trust</Issuer>
      <samlp:Status>
        <samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" /></samlp:Status>
      <Assertion ID="_949bf"
                 IssueInstant="201997Z"
                 Version="2.0"
                 xmlns="urn:oasis:names:tc:SAML:2.0:assertion">
        <Issuer>sites/trust</Issuer>
        <ds:Signature xmlns:ds="http:/sig#">
          <ds:SignedInfo>
            <ds:CanonicalizationMethod Algorithm="http://ww14n#" />
            <ds:SignatureMethod Algorithm="http://www256" />
            <ds:Reference URI="#f">
              <ds:Transforms>
                <ds:Transform Algorithm="http://www.w3.org/" />
                <ds:Transform Algorithm="http://www.w3.org/#" /></ds:Transforms>
              <ds:DigestMethod Algorithm="http://www.w3.org" />
              <ds:DigestValue>dig=</ds:DigestValue>
            </ds:Reference>
          </ds:SignedInfo>
          <ds:SignatureValue>
    sig/
    </ds:SignatureValue>
          <KeyInfo
          xmlns="http://www.w3.org/2000/09/xmldsig#">
            <ds:X509Data>
              <ds:X509Certificate>
    cert+YCo+==
    </ds:X509Certificate>
            </ds:X509Data>
            </KeyInfo>
        </ds:Signature>
        <Subject>
          <NameID>id</NameID>
          <SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
            <SubjectConfirmationData NotOnOrAfter="2018-12-05T13:11:26.997Z" />
          </SubjectConfirmation>
        </Subject>
        <Conditions NotBefore="2018-12-05T13:06:26.994Z"
                    NotOnOrAfter="2018-12-05T14:06:26.994Z">
        </Conditions>
        <AttributeStatement>
          <Attribute Name="http://schemas.xmlsoap.org/claims/Group">
            <AttributeValue>GET_THIS_VALUE</AttributeValue>
          </Attribute>
        </AttributeStatement>
        <AuthnStatement AuthnInstant="2018-12-05T13:05:21.872Z"
                        SessionIndex="_918c9d27-b2f3-4cf3-b5ad-9a0f049249bf">
          <AuthnContext>
            <AuthnContextClassRef>protectt</AuthnContextClassRef>
          </AuthnContext>
        </AuthnStatement>
      </Assertion>
    </samlp:Response>

    通过执行以下操作,我成功获得了“urn:oasis:names:tc:SAML:2.0:status:Success”的值:

    var status = document.descendantWithPath("samlp:Status").firstChild.attr;
    console.log("status id:", status['Value']);
    

    如何获取AttributeStatement下的值>属性名称>属性值。我要生成的值在XML中命名为GET_THIS_value。

    用于创建var文档的代码

    const body = req.body.SAMLResponse;
    var b = new Buffer(body, 'base64');
    let text = b.toString('ascii');
    console.log('formmatted saml', text);
    var document = new xmldoc.XmlDocument(text);
    

    奥多姆来自帮助

    Dom {
      rawHTML:
    '<samlp:Response ID="_146e55d5-5f43-443a-a1b5-5ef2f09ad06d" Version="2.0" IssueInstant="2018-12-06T14:16:27.985Z" Destination="httpcom" Consent="urn:oasis:names:tc:SAML:2.0:consent:unspecified" InResponseTo="_6b17407f6451fc54076e" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"><Issuer xmlns="urn:oasis:names:tc:SAML:2.0:assertion">htttrust</Issuer><samlp:Status><samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" /></samlp:Status><Assertion ID="_e90c2462-47e1-4e13-ac87-10e7d0aca2ff" IssueInstant="2018-12-06T14:16:27.985Z" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:assertion"><Issuer>http://setrust</Issuer><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/2001/04/xmldsig-more#rsa-sha256" /><ds:Reference URI="#_e90c2462-47e1-4e13-ac87-10e7d0aca2ff"><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/2001/04/xmlenc#sha256" /><ds:DigestValue>QRT3c4m1p4458PF7rYlbkBX+tqwyscg2nelBkpBWqn4=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>dZkCndDEU5TSuFMcioRqgNkjqt1rv3nzmupbYu3RZxNd2hIR5116Mm9y/C8TnK7F6KC6BVBLLJ1BfonMuumFTcCu9hke2U29a/XqhOpRFM2EGde6wWaPgDV2zjfQLSLjO1Tkj7jTe35vIeg5WZhdi91qHOvDSPcH3WcvbslNMHycLnPMANq0MsSO503VW7EsJNPRB8uEkBxAoCOPKG4URojk5+IBeKHHrKREKEJXDss+kiLNgi2flsjVjPpXk8fsMBOgJnSJ1Qjqpu/cAUw8fyn2byxMh7x4wxsBu+pbHm8lNprbmmxRwT4wtiSuQnEDtadeH7ocVJWPWKlEFsaR/w==</ds:SignatureValue><KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"><ds:X509Data><ds:X509Certificate>MIIC1jCCAb6gAwIBAgIQPvWwoUDezpBMDadtU7hS5jANBgkqhkiG9w0BAQsFADAnMSUwIwYDVQQDExxBREZTIFNpZ25pbmcgLSBzdHMuaGVydHoubmV0MB4XDTE3MDYxMjE1MDUyNVoXDTE5MDYxMjE1MDUyNVowJzElMCMGA1UEAxMcQURGUyBTaWduaW5nIC0gc3RzLmhlcnR6Lm5ldDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJXb56TyUfhianHvhHFFK4C5cETsH15fG2+s9rSUrRDIEpHIECMR6PRayzed/Yi8J7wgFkzMgnYUUGB4F0p8/TC8ruvY+CwXGP/BEEvE2N6WuZF9bWNuT304XvVZleZpfoB+RMhwzel+gHN5zaCNvlikEEgCsGoGtsVDidGBF57KgmJQcICIEf1u6VD6QrCAyahxrEKoAJU7t3VpSwYNLlqKGCYCo+dn09rk7RZFuHKdBFQQ8NjQZCUzhK9c64crv6ms8polQUNPldeapEsF1SFBPOfElG+N/17sg4UQ2BVbxFAFoH2eES2OFFn6oWEAi81oh+2iVuIVwDwhZE8cXIUCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAOb3GC2O2SPntVbZU7HoxgEcgWw7TDFXRxegE5GdnsyXIVEdSSzPH2g+Tc+Uaz5DZqEe/XtzGqAxItAe3f/94lZyL4WPkDaWtns44cEpO9q0Pf/GFbZeNHSeXgz1r9KIgTuETZ76eSCeHF4RZ2ZOQOxe4f2kY1l6ugRJqSKTEjqTXfZCvt/PVlW1LZegcMTuStCzQZ3rL/4lYqUa/ez7H8yEsLOVXTgWMxiEhk0VPomXJSWtrLjFsVBqB6QCh1RGGlR0uO3Sn6HAINiWravjM8XOaLAn/cM9L7m8XDVH6+oEutH1sST9fb/OwwJFEj4sU8jwQN0Z+KX7lC6fq121bGw==</ds:X509Certificate></ds:X509Data></KeyInfo></ds:Signature><Subject><NameID>NH37782</NameID><SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"><SubjectConfirmationData InResponseTo="_6b17407f6451fc54076e" NotOnOrAfter="2018-12-06T14:21:27.985Z" Recipient="httrtz.com" /></SubjectConfirmation></Subject><Conditions NotBefore="2018-12-06T14:16:27.983Z" NotOnOrAfter="2018-12-06T15:16:27.983Z"><AudienceRestriction><Audience>.com</Audience></AudienceRestriction></Conditions><AttributeStatement><Attribute Name="http://schemas.xmlsoap.org/claims/Group"><AttributeValue>net</AttributeValue></Attribute></AttributeStatement><AuthnStatement AuthnInstant="2018-12-06T14:16:27.881Z" SessionIndex="_e90c2462-47e1-4e13-ac87-10e7d0aca2ff"><AuthnContext><AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</AuthnContextClassRef></AuthnContext></AuthnStatement></Assertion></samlp:Response>' }
    1 回复  |  直到 7 年前
        1
  •  0
  •   mplungjan    7 年前

    这在Chrome中工作

    var status = document.querySelector("samlp\\:StatusCode").getAttribute("Value");
    console.log(status);
    var attrVal = document.querySelector("AttributeValue").textContent;
    console.log(attrVal)
    <samlp:Response ID="ffff" Version="2.0" IssueInstant="207Z" Destination="htm" Consent="urn:oasis:names:tc:SAML:2.0:consent:unspecified" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol">
      <Issuer xmlns="urn:oasis:names:tc:SAML:2.0:assertion">sites/trust</Issuer>
      <samlp:Status>
        <samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" />
      </samlp:Status>
      <Assertion ID="_949bf" IssueInstant="201997Z" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:assertion">
        <Issuer>sites/trust</Issuer>
        <ds:Signature xmlns:ds="http:/sig#">
          <ds:SignedInfo>
            <ds:CanonicalizationMethod Algorithm="http://ww14n#" />
            <ds:SignatureMethod Algorithm="http://www256" />
            <ds:Reference URI="#f">
              <ds:Transforms>
                <ds:Transform Algorithm="http://www.w3.org/" />
                <ds:Transform Algorithm="http://www.w3.org/#" /></ds:Transforms>
              <ds:DigestMethod Algorithm="http://www.w3.org" />
              <ds:DigestValue>dig=</ds:DigestValue>
            </ds:Reference>
          </ds:SignedInfo>
          <ds:SignatureValue>
            sig/
          </ds:SignatureValue>
          <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
            <ds:X509Data>
              <ds:X509Certificate>
                cert+YCo+==
              </ds:X509Certificate>
            </ds:X509Data>
          </KeyInfo>
        </ds:Signature>
        <Subject>
          <NameID>id</NameID>
          <SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
            <SubjectConfirmationData NotOnOrAfter="2018-12-05T13:11:26.997Z" />
          </SubjectConfirmation>
        </Subject>
        <Conditions NotBefore="2018-12-05T13:06:26.994Z" NotOnOrAfter="2018-12-05T14:06:26.994Z">
        </Conditions>
        <AttributeStatement>
          <Attribute Name="http://schemas.xmlsoap.org/claims/Group">
            <AttributeValue>GET_THIS_VALUE</AttributeValue>
          </Attribute>
        </AttributeStatement>
        <AuthnStatement AuthnInstant="2018-12-05T13:05:21.872Z" SessionIndex="_918c9d27-b2f3-4cf3-b5ad-9a0f049249bf">
          <AuthnContext>
            <AuthnContextClassRef>protectt</AuthnContextClassRef>
          </AuthnContext>
        </AuthnStatement>
      </Assertion>
    </samlp:Response>

    使用XMLDoc:

    var text = `<samlp:Response ID="ffff" Version="2.0" IssueInstant="207Z" Destination="htm" Consent="urn:oasis:names:tc:SAML:2.0:consent:unspecified" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol">
      <Issuer xmlns="urn:oasis:names:tc:SAML:2.0:assertion">sites/trust</Issuer>
      <samlp:Status>
        <samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" />
      </samlp:Status>
      <Assertion ID="_949bf" IssueInstant="201997Z" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:assertion">
        <Issuer>sites/trust</Issuer>
        <ds:Signature xmlns:ds="http:/sig#">
          <ds:SignedInfo>
            <ds:CanonicalizationMethod Algorithm="http://ww14n#" />
            <ds:SignatureMethod Algorithm="http://www256" />
            <ds:Reference URI="#f">
              <ds:Transforms>
                <ds:Transform Algorithm="http://www.w3.org/" />
                <ds:Transform Algorithm="http://www.w3.org/#" /></ds:Transforms>
              <ds:DigestMethod Algorithm="http://www.w3.org" />
              <ds:DigestValue>dig=</ds:DigestValue>
            </ds:Reference>
          </ds:SignedInfo>
          <ds:SignatureValue>
            sig/
          </ds:SignatureValue>
          <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
            <ds:X509Data>
              <ds:X509Certificate>
                cert+YCo+==
              </ds:X509Certificate>
            </ds:X509Data>
          </KeyInfo>
        </ds:Signature>
        <Subject>
          <NameID>id</NameID>
          <SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
            <SubjectConfirmationData NotOnOrAfter="2018-12-05T13:11:26.997Z" />
          </SubjectConfirmation>
        </Subject>
        <Conditions NotBefore="2018-12-05T13:06:26.994Z" NotOnOrAfter="2018-12-05T14:06:26.994Z">
        </Conditions>
        <AttributeStatement>
          <Attribute Name="http://schemas.xmlsoap.org/claims/Group">
            <AttributeValue>GET_THIS_VALUE</AttributeValue>
          </Attribute>
        </AttributeStatement>
        <AuthnStatement AuthnInstant="2018-12-05T13:05:21.872Z" SessionIndex="_918c9d27-b2f3-4cf3-b5ad-9a0f049249bf">
          <AuthnContext>
            <AuthnContextClassRef>protectt</AuthnContextClassRef>
          </AuthnContext>
        </AuthnStatement>
      </Assertion>
    </samlp:Response>`;
    
    var oParser = new DOMParser();
    var oDOM = oParser.parseFromString(text, "application/xml");
    var doc = oDOM.documentElement;
    var statusByAttribute = doc.querySelector("[Value]").getAttribute("Value");
    // var statusByTagName = document.querySelector("samlp\\:StatusCode").getAttribute("Value");
    console.log(statusByAttribute);
    var attrVal = doc.querySelector("AttributeValue").textContent;
    console.log(attrVal)