从Java中的SOAPMessage获取原始XML
我已经在JAX-WS中build立了SOAP WebServiceProvider,但是我很难弄清楚如何从SOAPMessage(或任何Node)对象获取原始XML。 下面是我现在得到的代码示例,以及我试图抓取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? 如果通过设置不同types的Provider(如Source)来获取原始XML,我也愿意这样做。
你可以试试这种方式。
SOAPMessage msg = messageContext.getMessage(); ByteArrayOutputStream out = new ByteArrayOutputStream(); msg.writeTo(out); String strMsg = new String(out.toByteArray());
如果您有SOAPMessage
或SOAPMessageContext
,则可以使用Transformer
,通过将其转换为通过DOMSource
的Source
:
final SOAPMessage message = messageContext.getMessage(); final StringWriter sw = new StringWriter(); try { TransformerFactory.newInstance().newTransformer().transform( new DOMSource(message.getSOAPPart()), new StreamResult(sw)); } catch (TransformerException e) { throw new RuntimeException(e); } // Now you have the XML as a String: System.out.println(sw.toString());
这将考虑到编码,所以你的“特殊字符”不会被弄乱。
事实certificate,可以通过使用Provider <Source>来获取原始XML,这样:
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; } }
我已经不需要这个解决scheme了,所以我没有亲自尝试这个代码 – 它可能需要一些调整才能正确。 但是我知道这是从Web服务中获取原始XML的正确途径。
(如果你绝对必须有一个SOAPMessage对象,我不确定如何使这个工作成为可能,但是如果你要处理原始的XML,为什么还要使用更高级的对象?)
只是为了debugging目的,使用一行代码 –
msg.writeTo(System.out);
如果您需要将xmlstring格式化为xml,请尝试以下操作:
String xmlStr = "your-xml-string"; Source xmlInput = new StreamSource(new StringReader(xmlStr)); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(xmlInput, new StreamResult(new FileOutputStream("response.xml")));
使用变压器工厂: –
public static String printSoapMessage(final SOAPMessage soapMessage) throws TransformerFactoryConfigurationError, TransformerConfigurationException, SOAPException, TransformerException { final TransformerFactory transformerFactory = TransformerFactory.newInstance(); final Transformer transformer = transformerFactory.newTransformer(); // Format it transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); final Source soapContent = soapMessage.getSOAPPart().getContent(); final ByteArrayOutputStream streamOut = new ByteArrayOutputStream(); final StreamResult result = new StreamResult(streamOut); transformer.transform(soapContent, result); return streamOut.toString(); }
如果你有客户端代码,那么你只需要添加以下两行来获得XML请求/响应。 这里_call
是org.apache.axis.client.Call
String request = _call.getMessageContext().getRequestMessage().getSOAPPartAsString(); String response = _call.getMessageContext().getResponseMessage().getSOAPPartAsString();
这工作
final StringWriter sw = new StringWriter(); try { TransformerFactory.newInstance().newTransformer().transform( new DOMSource(soapResponse.getSOAPPart()), new StreamResult(sw)); } catch (TransformerException e) { throw new RuntimeException(e); } System.out.println(sw.toString()); return sw.toString();