有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

NodeList在JAVA中返回带null的XML

我试图在以下步骤中解析XML,但返回的XML为空

步骤1:我得到以下XML字符串作为响应

    <?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <saml1p:Response InResponseTo="123456789" IssueInstant="2020-12-03T02:11:31.675Z" MajorVersion="1" MinorVersion="1" ResponseID="_b7dfe0c3d6d5288e03089058e9c47640" xmlns:saml1p="urn:oasis:names:tc:SAML:1.0:protocol">
            <saml1p:Status>
                <saml1p:StatusCode Value="saml1p:Success" />
            </saml1p:Status>
            <saml1:Assertion AssertionID="_8d6e176b1c2b4b5822bdcd001c1453f9" IssueInstant="2020-12-03T02:11:31.675Z" Issuer="localhost" MajorVersion="1" MinorVersion="1" xmlns:saml1="urn:oasis:names:tc:SAML:1.0:assertion">
                <saml1:Conditions NotBefore="2020-12-03T02:11:31.675Z" NotOnOrAfter="2020-12-03T02:12:01.675Z">
                    <saml1:AudienceRestrictionCondition>
                        <saml1:Audience>https://URL</saml1:Audience>
                    </saml1:AudienceRestrictionCondition>
                </saml1:Conditions>
                <saml1:AuthenticationStatement AuthenticationInstant="2020-12-03T00:55:05.315Z" AuthenticationMethod="urn:oasis:names:tc:SAML:1.0:am:password">
                    <saml1:Subject>
                        <saml1:NameIdentifier>xxx</saml1:NameIdentifier>
                        <saml1:SubjectConfirmation>
                            <saml1:ConfirmationMethod>urn:oasis:names:tc:SAML:1.0:cm:artifact</saml1:ConfirmationMethod>
                        </saml1:SubjectConfirmation>
                    </saml1:Subject>
                </saml1:AuthenticationStatement>
                <saml1:AttributeStatement>
                    <saml1:Subject>
                        <saml1:NameIdentifier>xxx</saml1:NameIdentifier>
                        <saml1:SubjectConfirmation>
                            <saml1:ConfirmationMethod>urn:oasis:names:tc:SAML:1.0:cm:artifact</saml1:ConfirmationMethod>
                        </saml1:SubjectConfirmation>
                    </saml1:Subject>
                    <saml1:Attribute AttributeName="displayLastName" AttributeNamespace="http://www.ja-sig.org/products/cas/">
                        <saml1:AttributeValue>xxx</saml1:AttributeValue>
                    </saml1:Attribute>
                    <saml1:Attribute AttributeName="displayFirstName" AttributeNamespace="http://www.ja-sig.org/products/cas/">
                        <saml1:AttributeValue>xxx</saml1:AttributeValue>
                    </saml1:Attribute>
                    <saml1:Attribute AttributeName="mail" AttributeNamespace="http://www.ja-sig.org/products/cas/">
                        <saml1:AttributeValue>xxx@gmail.com</saml1:AttributeValue>
                    </saml1:Attribute>
                    <saml1:Attribute AttributeName="sAMAccountName" AttributeNamespace="http://www.ja-sig.org/products/cas/">
                        <saml1:AttributeValue>xxx</saml1:AttributeValue>
                    </saml1:Attribute>
                    <saml1:Attribute AttributeName="displayName" AttributeNamespace="http://www.ja-sig.org/products/cas/">
                        <saml1:AttributeValue>xxx</saml1:AttributeValue>
                    </saml1:Attribute>
                    <saml1:Attribute AttributeName="givenName" AttributeNamespace="http://www.ja-sig.org/products/cas/">
                        <saml1:AttributeValue>xxx</saml1:AttributeValue>
                    </saml1:Attribute>
                    <saml1:Attribute AttributeName="description" AttributeNamespace="http://www.ja-sig.org/products/cas/">
                        <saml1:AttributeValue>DEPRECATED</saml1:AttributeValue>
                    </saml1:Attribute>
                    <saml1:Attribute AttributeName="distinguishedName" AttributeNamespace="http://www.ja-sig.org/products/cas/">
                        <saml1:AttributeValue>xxx</saml1:AttributeValue>
                    </saml1:Attribute>
                    <saml1:Attribute AttributeName="employeeID" AttributeNamespace="http://www.ja-sig.org/products/cas/">
                        <saml1:AttributeValue>xxx</saml1:AttributeValue>
                    </saml1:Attribute>
                    <saml1:Attribute AttributeName="cn" AttributeNamespace="http://www.ja-sig.org/products/cas/">
                        <saml1:AttributeValue>xxx</saml1:AttributeValue>
                    </saml1:Attribute>
                    <saml1:Attribute AttributeName="pepRole1" AttributeNamespace="http://www.ja-sig.org/products/cas/">
                        <saml1:AttributeValue>xxx</saml1:AttributeValue>
                    </saml1:Attribute>
                    <saml1:Attribute AttributeName="memberOf" AttributeNamespace="http://www.ja-sig.org/products/cas/">
 
                    </saml1:Attribute>
                    <saml1:Attribute AttributeName="sn" AttributeNamespace="http://www.ja-sig.org/products/cas/">
                        <saml1:AttributeValue>xxx</saml1:AttributeValue>
                    </saml1:Attribute>
                    <saml1:Attribute AttributeName="department" AttributeNamespace="http://www.ja-sig.org/products/cas/">
                        <saml1:AttributeValue>xxx</saml1:AttributeValue>
                    </saml1:Attribute>
                    <saml1:Attribute AttributeName="group" AttributeNamespace="http://www.ja-sig.org/products/cas/">
                        <saml1:AttributeValue>DEPRECATED</saml1:AttributeValue>
                    </saml1:Attribute>
                </saml1:AttributeStatement>
            </saml1:Assertion>
        </saml1p:Response>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

步骤2:我使用文档生成器构建XML

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    InputSource iSource = new InputSource(new StringReader(xmlResponse));
    Document doc = dBuilder.parse(iSource);
    doc.getDocumentElement().normalize();

步骤3:在执行下面的代码后,我获取节点并修改saml1p:Response的属性值,返回的XML为null

//Begin Issue seems to be in this block of code
NodeList samlResponse = doc.getElementsByTagName("saml1p:Response");
Element samlResponse = (Element) samlResponses.item(0);
samlResponse.setAttribute("InResponseTo", "_" + samlResponse.getAttribute("InResponseTo"));
//End Issue seems to be in this block of code
doc.getDocumentElement().normalize();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
transformer.transform(source, new StreamResult(output));

上面的代码返回null,你知道为什么会出现这个问题吗

有趣的是,这段代码可以在生产环境中使用,但不能在我们的测试环境中使用


共 (0) 个答案