java 接收soap,如何从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.

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?

解决方案

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.

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.

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.

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
接收SOAP的参数,需要使用JavaSOAP库,例如JAX-WS或Apache Axis。以下是一个简单的示例: 假设我们有一个SOAP消息,其包含一个名为“name”的参数,我们要在Java接收它。 1. 首先,我们需要创建一个Java类来表示SOAP消息的参数: ``` public class MySoapRequest { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } ``` 2. 接下来,我们需要创建一个Java类来处理SOAP消息,并使用JAX-WS或Apache Axis将其绑定到SOAP消息: 使用JAX-WS: ``` @WebService public class MySoapService { @WebMethod public void processSoapRequest(MySoapRequest request) { String name = request.getName(); // 处理参数 } } ``` 使用Apache Axis: ``` public class MySoapService { public void processSoapRequest(MySoapRequest request) { String name = request.getName(); // 处理参数 } } // 在服务器端配置 AxisServlet servlet = new AxisServlet(); servlet.setOption("className", "MySoapService"); ``` 3. 最后,我们需要将SOAP消息发送到Java服务端,并从提取参数: 使用JAX-WS: ``` // 创建SOAP消息 MySoapRequest request = new MySoapRequest(); request.setName("John"); // 创建SOAP客户端 MySoapService service = new MySoapService(); MySoapServicePort port = service.getMySoapServicePort(); // 调用SOAP方法并传递参数 port.processSoapRequest(request); ``` 使用Apache Axis: ``` // 创建SOAP消息 MySoapRequest request = new MySoapRequest(); request.setName("John"); // 创建SOAP客户端 Call call = new Call(); call.setTargetEndpointAddress(new URL("http://localhost:8080/MySoapService")); call.setOperationName(new QName("processSoapRequest")); // 调用SOAP方法并传递参数 call.invoke(new Object[]{request}); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值