如何将SOAPBody转换为String [英] How to convert SOAPBody to String

查看:178
本文介绍了如何将SOAPBody转换为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将SOAPBody转换为String。最好的方法是什么?
我应该首先将其转换为xml然后将其转换为String,或者我们可以将其转换为String。

I want to convert SOAPBody to String. What is the best way to do it? Should i first convert it to xml and then convert it into String or we can jsut convert it into String.

推荐答案

从SOAPMessage开始时,最简单的方法是使用 writeTo 方法:

When starting from a SOAPMessage, the easiest way is to use the writeTo method :

ByteArrayOutputStream stream = new ByteArrayOutputStream();
soapMessage.writeTo(stream);
String message = new String(stream.toByteArray(), "utf-8") 

(上面,我假设您的SAAJ实现将使用UTF-8,您可能想要检查)。

(Above, I assume your SAAJ implementation will use UTF-8, you'd probably want to check).

如果从SOAPBody开始,那么您可能应该使用XML API,看到SOAPBody是一个org.w3.dom.Element,最简单的方法可能是使用TrAX:

If starting from a SOAPBody, then you probably should use XML APIs, seeing SOAPBody is a org.w3.dom.Element, the easiest way would probably be using TrAX :

SOAPBody element = ... // Whatever
DOMSource source = new DOMSource(element);
StringWriter stringResult = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform(source, new StreamResult(stringResult));
String message = stringResult.toString();

(对不起,我这里没有我的IDE,无法检查这是否编译,但是应该非常接近)。

(Sorry I do not have my IDE right here, can not check if this compiles, but that should be pretty close).

请注意:序列化的SOAPMessage可能不是原始XML:它可能是MIME结构:如果SOAPMessage实际使用SwA(SOAP With Attachment)或MTOM。但是,SOAPBody绝对是纯XML。

Please note : A serialized SOAPMessage may not be raw XML : it might be a MIME structure : if the SOAPMessage actually uses SwA (SOAP With Attachment) or MTOM. However, SOAPBody is definitely pure XML.

这篇关于如何将SOAPBody转换为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆