【问题标题】:Get XML Element with JDOM使用 JDOM 获取 XML 元素
【发布时间】:2014-09-03 05:49:33
【问题描述】:

我有一个类似于以下的 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;
      } 
  }

【问题讨论】:

    标签: java xml saml jdom


    【解决方案1】:
      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 调用中使用正确的命名空间从根文档中检索它。

    【讨论】:

      猜你喜欢
      • 2012-08-25
      • 1970-01-01
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      相关资源
      最近更新 更多