作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从 .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/
我从 .net 服务获取以下 XML: true
我正在尝试使用 JAXM 创建一个调用第三方异步 SOAP API 的客户端。根据 JAXM 文档 ( https://docs.oracle.com/cd/E19644-01/817-5452/ws
我正在尝试学习不同的 Web 服务以了解它们的用途。但是,我很困惑,因为这些术语经常重叠。如果有人能简要地告诉我这些术语之间有什么区别,我将不胜感激。我遇到了一些来源,例如 http://java.s
我是一名优秀的程序员,十分优秀!