Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract data from a SOAP response in Java?

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?

like image 559
thedude19 Avatar asked Sep 23 '09 20:09

thedude19


2 Answers

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.

like image 148
brabster Avatar answered Nov 11 '22 02:11

brabster


It's strongly receommended that you not try and decode SOAP by hand :)

Just to expand on what @Brabster said, Netbeans has extensive Web Service support, especially using the JAX-WS library.

http://www.netbeans.org/kb/60/websvc/jax-ws.html#Exercise_3_1

like image 23
UberAlex Avatar answered Nov 11 '22 01:11

UberAlex