我有一个由应用程序生成的XML文本,我需要在其周围包装一个SOAP信封,然后进行Web服务调用.
以下代码构建了信封,但我不知道如何将现有XML数据添加到SOAPBody元素中.
String rawXml = "<some-data><some-data-item>1</some-data-item></some-data>";
// Start the API
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage request = mf.createMessage();
SOAPPart part = request.getSOAPPart();
SOAPEnvelope env = part.getEnvelope();
// Get the body. How do I add the raw xml directly into the body?
SOAPBody body = env.getBody();
Run Code Online (Sandbox Code Playgroud)
我尝试了body.addTextNode()但是它增加了内容,<而其他人则逃脱了.
Eva*_*tti 10
以下将XML添加为文档:
Document document = convertStringToDocument(rawXml);
body.addDocument(document);
Run Code Online (Sandbox Code Playgroud)
文件创作:
private static Document convertStringToDocument(String xmlStr) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
我convertStringToDocument()从这篇文章中汲取了逻辑.