我的尝试是从以下SOAPBody内容创建多个soap消息--只是一个示例消息,而不是实际的soap消息。每个EmpId都会有单独的请求。
<Request>
<EMPId>?</EMPId>
</Request>
我使用以下代码将上述请求字符串转换为文档对象。
DocumentBuilder parser = factory.newDocumentBuilder();
Document doc = parser.parse(new InputSource(new ByteArrayInputStream(xmlString.getBytes())));
一旦我有了文档,我就可以通过替换SOAPBody值来创建EMPId。
现在,我必须为每个创建的SOAPMessages创建单独的SOAPBody。
为此,我使用以下代码。
private static String cretaeSOAPMessage(Document soapBodyDoc, String serverURI, String soapAction){
String soapMsg = null;
try {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("v1",serverURI);
SOAPBody soapBody = envelope.getBody();
soapBodyDoc.setPrefix("v1");
soapBody.addDocument(soapBodyDoc);
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + soapAction);
soapMessage.saveChanges();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
soapMessage.writeTo(out);
} catch (IOException e) {
e.printStackTrace();
}
soapMsg = new String(out.toByteArray());
} catch (SOAPException e) {
e.printStackTrace();
}
return soapMsg;
}
但是,在执行内容'soapBodyDoc.setPrefix("v1");‘行时,我得到了以下错误
线程"main“org.w3c.dom.DOMException: NAMESPACE_ERR中的异常:尝试以与命名空间有关的不正确的方式创建或更改对象。
我试图在创建SOAPBody的地方添加名称空间预基,即使这种能力得到了实现。如何避免此错误并向SOAPBody添加命名空间前缀?
发布于 2014-08-25 07:01:36
解决了。名称空间声明从信封中移除并添加到正文中。
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody soapBody = envelope.getBody();
soapBody.addDocument(soapBodyDoc);
**soapBody.addNamespaceDeclaration("", serverURI);**
前缀被移除,因为原始前缀没有任何前缀。
https://stackoverflow.com/questions/25479797
复制相似问题