使用XML解析时显示未定义的文档元素

时间:2018-12-06 17:35:28

标签: javascript dom xml-parsing saml domparser

我的SAML响应包含在变量文本中。

我的解析器代码:

var oParser = new DOMParser();
var oDOM = oParser.parseFromString(text, "application/xml");

我的最终目标是从我的SAML响应中提取RETURN_THIS_VALUE的值。下面是变量oDom的结果。

oDom

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>RETURN_THIS_VALUE</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>' }

我尝试通过执行以下操作将oDom变量转换为文档:

var doc = oDOM.documentElement;

但是如果我在变量 doc 上执行console.log,我将返回未定义状态。有什么想法吗?

在console.log上文本变量的外观示例

<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>

1 个答案:

答案 0 :(得分:0)

您的代码可以正常工作:

var text = '<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+pbHm8lNprb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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>RETURN_THIS_VALUE</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>';

var oParser = new DOMParser();
var oDOM = oParser.parseFromString(text, "application/xml");

var doc = oDOM.querySelector('AttributeValue').textContent;

console.log(doc);