2014-09-03 90 views
1

我有一个如下所示的XML文件。使用JDOM获取XML元素

<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" 
        Destination="https://hostname.example.com:4444/fed/idp/samlv20" 
        ID="id-OPIfhD3il2eK816THPlj2Nk38KM-" 
        IssueInstant="2014-09-02T14:36:02Z" 
        ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" 
        Version="2.0" 
        > 
    <saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" 
       Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity" 
       >https://federation.example.com/sp</saml:Issuer> 
    <samlp:NameIDPolicy AllowCreate="true" 
         Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified" 
         /> 
    <samlp:RequestedAuthnContext Comparison="exact"> 
     <saml:AuthnContextClassRef xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</saml:AuthnContextClassRef> 
    </samlp:RequestedAuthnContext> 
</samlp:AuthnRequest> 

怎样才能使用JDOM上面的XML内容 “https://federation.example.com/sp”?

我正在尝试下面的代码,并能够成功地检索“IssueInstant”&“ProtocolBinding”。但无法检索内容“https://federation.example.com/sp”。请帮忙。

private String[] getRequestAttributes(String xmlString) throws SamlException { 
     Document doc = Util.createJdomDoc(xmlString); 
     if (doc != null) { 
     String[] samlRequestAttributes = new String[2]; 
     samlRequestAttributes[0] = doc.getRootElement().getAttributeValue(
      "IssueInstant"); 
     samlRequestAttributes[2] = doc.getRootElement().getAttributeValue(
      "ProtocolBinding"); 
     return samlRequestAttributes; 
     } 
    } 

回答

0
private String[] getRequestAttributes(String xmlString) throws SamlException { 
     Document doc = Util.createJdomDoc(xmlString); 
     if (doc != null) { 
     String[] samlRequestAttributes = new String[2]; 
     samlRequestAttributes[0] = doc.getRootElement().getAttributeValue(
      "IssueInstant"); 
     samlRequestAttributes[2] = doc.getRootElement().getAttributeValue(
      "ProtocolBinding"); 
     return samlRequestAttributes; 
     } 
    } 

上面的代码是行不通的,因为几个原因。首先,samlRequestAttributes[2] = ...行会抛出一个ArrayIndexOutOfBounds异常,因为没有索引值2.您应该使用索引1.

您不指示要存储值https://federation.example.com/sp的位置,但我认为您会希望它在返回数组的某处(可能在索引1?)。

检索它的方法是正确使用XML的命名空间。

考虑以下几点:

private String[] getRequestAttributes(String xmlString) throws SamlException { 
     Document doc = Util.createJdomDoc(xmlString); 
     if (doc != null) { 
     Namespace saml = Namespace.get("urn:oasis:names:tc:SAML:2.0:assertion"); 
     String[] samlRequestAttributes = new String[3]; 
     Element root = doc.getRootElement(); 
     samlRequestAttributes[0] = root.getAttributeValue("IssueInstant"); 
     samlRequestAttributes[1] = root.getAttributeValue("ProtocolBinding"); 
     samlRequestAttributes[2] = root.getChild("Issuer", saml).getText(); 

     return samlRequestAttributes; 
     } 
    } 

注意你需要如何识别命名空间的子元素,并从根本上的文档与getChild调用正确的命名空间检索。

+0

你是我的救星。非常感谢这个明确的解释! – prasee 2014-09-05 09:10:38