简体   繁体   English

从Java C#Webservice获取单个数据

[英]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消息,我需要获取此响应的字符串,然后将其转换为图片,但问题是要开始使用字符串。

<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. 如何从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). (我熟悉发送SOAP消息,我也可以通过一些C&P获得一些消息,但我不知道如何使用它们)。

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. 如果需要,我可以提供更多信息,但这只是我无法阅读的许多类似SOAP响应的例子,它们带有一个单独的元素。

You could use jSoup and just do something like: 您可以使用jSoup并执行以下操作:

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. 基本上jSoup是java的一个库,它允许你使用类似jquery的选择器语法来查询大块的html / xml。

Here is a more in-detail piece of code and description: jSoup . 这是一个更详细的代码和描述: 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. 在我们得到SOAP请求的响应之后,我们所要做的就是将主体提取为Document,然后将第一个子元素的值作为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. 我希望它对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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