Java 如何将 SOAPBody 转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25149528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert SOAPBody to String
提问by ashish2k3
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.
我想将 SOAPBody 转换为 String。最好的方法是什么?我应该首先将其转换为 xml,然后将其转换为 String,或者我们可以将其转换为 String。
回答by GPI
When starting from a SOAPMessage, the easiest way is to use the writeTo
method :
从 SOAPMessage 开始时,最简单的方法是使用writeTo
方法:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
soapMessage.writeTo(stream);
String message = new String(stream.toByteArray(), "utf-8")
(Above, I assume your SAAJ implementation will use UTF-8, you'd probably want to check).
(以上,我假设您的 SAAJ 实现将使用 UTF-8,您可能需要检查一下)。
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 开始,那么您可能应该使用 XML API,看到 SOAPBody 是一个 org.w3.dom.Element,最简单的方法可能是使用 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();
(Sorry I do not have my IDE right here, can not check if this compiles, but that should be pretty close).
(抱歉,我这里没有我的 IDE,无法检查它是否可以编译,但这应该非常接近)。
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.
请注意:序列化的 SOAPMessage 可能不是原始 XML:它可能是 MIME 结构:如果 SOAPMessage 实际使用 SwA(带附件的 SOAP)或 MTOM。但是,SOAPBody 绝对是纯 XML。
回答by Atanu Mukherjee
Figured this might help -
认为这可能会有所帮助 -
private String convertToString (SOAPBody message) throws Exception{
Document doc = message.extractContentAsDocument();
StringWriter sw = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new DOMSource(doc), new StreamResult(sw));
return sw.toString();
}
Thanks to the following post - XML Document to String?
感谢以下帖子 - XML 文档到字符串?
回答by user07
You do not need to convert SOAPBody
to XML
, because it implements org.w3c.dom.Element
interface, thus this is already a valid XML
object.
You can use org.w3c.dom.ls
package to achieve your goal:
您不需要转换SOAPBody
为XML
,因为它实现了org.w3c.dom.Element
接口,因此这已经是一个有效的XML
对象。您可以使用org.w3c.dom.ls
包来实现您的目标:
String xmlAsString = null;
Element element = what-ever-element;
DOMImplementationLS domImplementationLS = (DOMImplementationLS)element.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
LSSerializer serializer = domImplementationLS.createLSSerializer();
xmlAsString = serializer.writeToString(element);
You can play with serializer.getDomConfig().setParameter(....)
to configure the serializer.
您可以玩serializer.getDomConfig().setParameter(....)
以配置序列化程序。