在Java中从SOAPMessage获取原始XML [英] Getting Raw XML From SOAPMessage in Java
问题描述
我在JAX-WS中设置了一个SOAP WebServiceProvider,但是我无法弄清楚如何从SOAPMessage(或任何Node)对象中获取原始XML。以下是我现在获得的代码示例,以及我试图获取XML的地方:
I've set up a SOAP WebServiceProvider in JAX-WS, but I'm having trouble figuring out how to get the raw XML from a SOAPMessage (or any Node) object. Here's a sample of the code I've got right now, and where I'm trying to grab the XML:
@WebServiceProvider(wsdlLocation="SoapService.wsdl")
@ServiceMode(value=Service.Mode.MESSAGE)
public class SoapProvider implements Provider<SOAPMessage>
{
public SOAPMessage invoke(SOAPMessage msg)
{
// How do I get the raw XML here?
}
}
有没有一种简单的方法来获取XML的原始要求?如果有办法通过设置不同类型的Provider来获取原始XML(例如Source),我也愿意这样做。
Is there a simple way to get the XML of the original request? If there's a way to get the raw XML by setting up a different type of Provider (such as Source), I'd be willing to do that, too.
推荐答案
事实证明,可以通过使用Provider< Source>来获取原始XML,这样:
It turns out that one can get the raw XML by using Provider<Source>, in this way:
import java.io.ByteArrayOutputStream;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.ws.Provider;
import javax.xml.ws.Service;
import javax.xml.ws.ServiceMode;
import javax.xml.ws.WebServiceProvider;
@ServiceMode(value=Service.Mode.PAYLOAD)
@WebServiceProvider()
public class SoapProvider implements Provider<Source>
{
public Source invoke(Source msg)
{
StreamResult sr = new StreamResult();
ByteArrayOutputStream out = new ByteArrayOutputStream();
sr.setOutputStream(out);
try {
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.transform(msg, sr);
// Use out to your heart's desire.
}
catch (TransformerException e) {
e.printStackTrace();
}
return msg;
}
}
我最终不需要这个解决方案,所以我自己并没有真正尝试过这个代码 - 可能需要进行一些调整才能做到正确。但我知道这是从Web服务获取原始XML的正确途径。
I've ended up not needing this solution, so I haven't actually tried this code myself - it might need some tweaking to get right. But I know this is the right path to go down to get the raw XML from a web service.
(如果绝对的话,我不知道如何使这个工作必须有一个SOAPMessage对象,但是如果你要处理原始XML,那么为什么你会使用更高级别的对象?)
(I'm not sure how to make this work if you absolutely must have a SOAPMessage object, but then again, if you're going to be handling the raw XML anyways, why would you use a higher-level object?)
这篇关于在Java中从SOAPMessage获取原始XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!