如何在java中解析来自SOAP响应消息的数据

发布于 2024-11-15 17:34:36 字数 272 浏览 6 评论 0原文

我正在使用 Dom4j 来解析 SOAP 消息。我遇到了一个问题:
Node 节点 = document.selectSingleNode( "/*/soapenv:Envelope/soapenv:Body/ns:addResponse" );
当我使用上述 XPath 时,出现以下异常:
异常:XPath 表达式使用未绑定的命名空间前缀 ns1
我找到了执行此操作的方法:删除名称空间,但不建议这样做。
任何帮助将不胜感激。谢谢
顺便说一句,这项工作有更好的方法或工具包吗?

I am using Dom4j to parse SOAP message. I had a problem:

Node node = document.selectSingleNode( "/*/soapenv:Envelope/soapenv:Body/ns:addResponse" );

when I use the above XPath, I got the following exception:

Exception: XPath expression uses unbound namespace prefix ns1

I found ways to do this: remove namespace, which is not recommended.

Any help will be appreciated. Thanks

BTW, is there a better way or toolkit for this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

标点 2024-11-22 17:34:36

您需要创建一个 org.dom4j.XPath 对象并向其附加合适的命名空间绑定。就像这样:

XPath xpath = document.createXPath("/*/soapenv:Envelope/soapenv:Body/ns:addResponse");
Map<String, String> nsb = new HashMap<String, String>();
nsb.put("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
nsb.put("ns", ".....");
Node node = xpath.selectSingleNode(document);

有更好的方法吗?嗯,现在有了 JAX-WS,如果您的服务有可用的 WSDL,那么为其生成 Java 接口和类通常会非常简单。

You need to create a org.dom4j.XPath object and attach suitable namespace bindings to it. Something like this:

XPath xpath = document.createXPath("/*/soapenv:Envelope/soapenv:Body/ns:addResponse");
Map<String, String> nsb = new HashMap<String, String>();
nsb.put("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
nsb.put("ns", ".....");
Node node = xpath.selectSingleNode(document);

Is there a better way? Well, these days there is JAX-WS, and if there is WSDL available for your service, it is usually very simple to generate Java interfaces and classes for it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文