2014-09-03 89 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