如何从Java中的SOAP响应中提取数据? [英] How to extract data from a SOAP response in Java?
问题描述
我有一个客户端设置为向国家气象服务SOAP服务器发送请求。我收到了我期望的回复,但我不确定从我需要的数据中提取数据的最佳方式。
I have a client set up to send a request to the National Weather Service SOAP server. I am receiving the response that I expect, but I am unsure as to the best way to extract the data from it that I need.
例如,有很多XML中的额外数据(在SOAPBody中),但我只想获取我设置的参数(例如温度)的数据到我的POJO。
For example, there is a lot of extra data in the XML (in the SOAPBody), but I only want to grab the data for the parameters that I set (such as temperature) to my POJO.
提取这些数据的最佳方法是什么?
What's the best way to extract this data?
推荐答案
我开始试图像你描述的那样手工使用SOAP Web服务 - 那里更好的方法。
I started out trying to consume SOAP Web Services by hand like you describe - there are better ways.
有些库可以为你完成所有工作 - 无需手工解析任何东西。
There are libraries out there that will do all the work for you - no need to parse anything by hand.
查看 JAX-WS 。大多数现代IDE(当然 Netbeans 和 Eclipse )还为构建Web服务客户端提供了点击支持给出一个WSDL。
Check out JAX-WS. Most Modern IDEs (Certainly Netbeans and Eclipse) also provide point and click support for building web service clients given a WSDL.
这条路线下最大的潜在问题是,如果没有WSDL,或者WSDL是错误的,在这种情况下,我链接的工具可能很难。
The biggest potential problem down this route is if there's no WSDL, or the WSDL is wrong, in which case the tooling I've linked might struggle.
下一个最安全的事情是使用像JAXP的SAX& DOM等(它们就在你的JRE中)解析响应,然后遍历所涉及的数据结构。
The next safest thing would be to use an XML Parser like JAXP's SAX & DOM etc (they're right there in your JRE) to parse the response and then walk the data structures involved.
最后你可以去使用拆分或正则表达式的字符串黑客攻击路径但是沿着这条路径存在很大的潜在痛苦 - XML规范还有嵌套标签。
Finally you could go the string hacking route using splits or regexes but down that path lies a great deal of potential pain - there's more to the XML spec then nested tags.
这篇关于如何从Java中的SOAP响应中提取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!