ffuentes ffuentes - 2 months ago 5
C# Question

Get a single piece of data from a SOAP C# Webservice in Java

I'm starting to deal with SOAP messages and I need to get this response's string to then convert it into a picture but the problem is to get the string to begin with.

<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>
<ObtenerImagenResponse xmlns="http://localhost/WebService">
<ObtenerImagenResult>This is a picture</ObtenerImagenResult>
</ObtenerImagenResponse>
</soap:Body>
</soap:Envelope>


How do I extract "This is a picture" from Java.

(I'm familiar with sending SOAP messages and I can get some messages too thanks to some C&P but I don't know how to work with all of them).

Thank you in advance. I can provide more information if needed but this is just an example of lots of similar SOAP responses I can't read and which carry one single element.

Answer

Well, the answer for this wasn't very complicated.

After we get the response of the SOAP request all we have to do is to extract the body as Document and then get the first child's value as a String.

SOAPBody sb = soapResponse.getSOAPBody();
Document XMLDoc = sb.extractContentAsDocument();
NodeList nl = XMLDoc.getElementsByTagName("ObtenerImagenResult");
String response = nl.item(0).getFirstChild().getNodeValue();
return response;

That's the best way to get that only item. I hope it's helpful for you.

Comments