gpt4 book ai didi

java - JAXM SOAP 消息解析

转载 作者:行者123 更新时间:2023-12-02 00:56:01 24 4
gpt4 key购买 nike

我从 .net 服务获取以下 XML:

<?xml version="1.0" encoding="utf-8"?>
<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>
<validateCredentialsResponse
xmlns="http://www.paragon.com/positionmonitor/PositionMonitor">
<validateCredentialsResult>
<ResultData xsi:type="ValidateCredentialsResultData">
<validated>true</validated>
<alreadyLoggedIn>false</alreadyLoggedIn>
</ResultData>
<Status>
<Condition xmlns="">SUCCESS</Condition>
<ErrorCode xmlns="">BO.00000</ErrorCode>
<ErrorDesc xmlns="">OK</ErrorDesc>
</Status>
</validateCredentialsResult>
</validateCredentialsResponse>
</soap:Body>
</soap:Envelope>

...我正在尝试使用 JAXM 解析它,但是以下内容始终计算为 null:

SOAPEnvelope env = reply.getSOAPPart().getEnvelope();

有人可以帮我吗?

最佳答案

我使用以下代码得到了很好的结果(至少需要 Java 1.5):

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Jaxm {

private static List<Element> elements(NodeList nodes) {
List<Element> result = new ArrayList<Element>(nodes.getLength());
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node instanceof Element)
result.add((Element)node);
}
return result;
}

private static Element named(Element elem, String name) {
if (!elem.getNodeName().equals(name))
throw new IllegalArgumentException("Expected " + name + ", got " + elem.getNodeName());
return elem;
}

@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException, SOAPException {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" \r\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \r\n" +
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n" +
" <soap:Body>\r\n" +
" <validateCredentialsResponse \r\n" +
" xmlns=\"http://www.paragon.com/positionmonitor/PositionMonitor\">\r\n" +
" <validateCredentialsResult>\r\n" +
" <ResultData xsi:type=\"ValidateCredentialsResultData\">\r\n" +
" <validated>true</validated>\r\n" +
" <alreadyLoggedIn>false</alreadyLoggedIn>\r\n" +
" </ResultData>\r\n" +
" <Status>\r\n" +
" <Condition xmlns=\"\">SUCCESS</Condition>\r\n" +
" <ErrorCode xmlns=\"\">BO.00000</ErrorCode>\r\n" +
" <ErrorDesc xmlns=\"\">OK</ErrorDesc>\r\n" +
" </Status>\r\n" +
" </validateCredentialsResult>\r\n" +
" </validateCredentialsResponse>\r\n" +
" </soap:Body>\r\n" +
"</soap:Envelope>\r\n" +
"";
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage msg = factory.createMessage(new MimeHeaders(), new ByteArrayInputStream(xml.getBytes(Charset.forName("UTF-8"))));
msg.saveChanges();
SOAPBody soapBody = msg.getSOAPBody();
for (Element response : elements(soapBody.getElementsByTagName("validateCredentialsResponse"))) {
for (Element result : elements(response.getElementsByTagName("validateCredentialsResult"))) {
List<Element> children = elements(result.getChildNodes());
Element resultData = named(children.get(0), "ResultData");
List<Element> resultDataChildren = elements(resultData.getChildNodes());
boolean validated = Boolean.getBoolean(named(resultDataChildren.get(0), "validated").getTextContent());
boolean alreadyLoggedIn = Boolean.getBoolean(named(resultDataChildren.get(1), "alreadyLoggedIn").getTextContent());
Element status = named(children.get(1), "Status");
List<Element> statusChildren = elements(status.getChildNodes());
String condition = named(statusChildren.get(0), "Condition").getTextContent();
String errorCode = named(statusChildren.get(1), "ErrorCode").getTextContent();
String errorDesc = named(statusChildren.get(2), "ErrorDesc").getTextContent();

System.out.printf("validated=%s alreadyLoggedIn=%s condition=%s errorCode=%s errorDesc=%s\n", validated, alreadyLoggedIn, condition, errorCode, errorDesc);
}
}
}
}

关于java - JAXM SOAP 消息解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/296202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com