简体   繁体   中英

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.

You could use jSoup and just do something like:

doc.select("ObtenerImagenResult");

Basically jSoup is a library for java which allows you to use a jquery-like selector syntax in order to query through big chunks of html/xml.

Here is a more in-detail piece of code and description: jSoup .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM