我正在尝试从 WS SOAP 响应中读取 2 个值。目前我无法这样做,我尝试了多种方法但没有成功。
我只对 OperationStatus 和 SessionId 感兴趣。
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<DoLoginResponse xmlns="censored">
<DoLoginResult>
<OperationStatus>true</OperationStatus>
<StatusMessage/>
<SecurityProfile>
<User>
<UserID>720eeac1-7134-4587-b81d-b7431718c51b</UserID>
</User>
<Session>
<SessionId>eb63f534-322f-4014-88e0-72e6a6a5b167</SessionId>
</Session>
<IsFirstLogon>false</IsFirstLogon>
<IsSystemOwner>true</IsSystemOwner>
</SecurityProfile>
<Authenticated>true</Authenticated>
<UpgradeRecommended>true</UpgradeRecommended>
</DoLoginResult>
</DoLoginResponse>
</soap:Body>
</soap:Envelope>
这是我到目前为止编写的代码。
SOAPBody sb = response.getSOAPBody();
Node LoginResponse = (Node)sb.getFirstChild();
Node LoginResult = (Node)LoginResponse.getFirstChild();
Node OperationStatus = (Node)LoginResult.getFirstChild();
if(OperationStatus.getFirstChild().getTextContent()=="true"){
_auth = true;
_Session = "";//get SessionId here
}else{
_auth = false;
return response;
}
出于某种原因,我永远不会进入我的 if 语句,因此我不知道如何完成此操作。
最佳答案
尝试使用 getElementsByTagName 访问节点并使用 equals 方法比较字符串值。
节点OperationStatus = sb.getElementsByTagName("OperationStatus").item(0);
if("true".equals(OperationStatus.getFirstChild().getTextContent()))
...
关于java - 获取 SOAP 响应JAVA中的节点值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37308585/