命名空间相关的错误,创建SOAP请求 [英] Namespace related error on creating SOAP Request
问题描述
我尝试从以下SOAPBody内容创建多个soap消息[只是一个示例,没有实际]。每个EmpId将有单独的请求。
My attempt is to create multiple soap messages from the following SOAPBody content[just a sample one, no the actual]. There will be seperate requests for each EmpId.
<Request>
<EMPId>?</EMPId>
</Request>
我使用以下代码将上述请求字符串转换为文档对象。
I use the following code to convert the above request string to a Document Object.
DocumentBuilder parser = factory.newDocumentBuilder();
Document doc = parser.parse(new InputSource(new ByteArrayInputStream(xmlString.getBytes())));
一旦我拥有该文档,我可以通过替换EMPId值来创建SOAPBody。
Once i have the document, i can create SOAPBody by substituting the EMPId values.
现在我必须为每个创建的SOAPBody创建单独的SOAPMessage。
Now i have to creare individual SOAPMessages for each SOAPBody created.
为此,我使用以下代码。
For that I use the following code.
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:尝试以
的方式创建或更改对象,就命名空间而言不正确。
Exception in thread "main" org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
我尝试添加命名空间prefic,我创建了SOAPBody,即使是dint。
我如何避免这个错误,并在SOAPBody中添加命名空间前缀?
I tried to add the namespace prefic where i create the SOAPBody, even that dint worked out. How can i avoid this error and add namespace prefix to the SOAPBody?
推荐答案
得到解决。
命名空间声明从信封中删除并添加到正文。
Got it resolved. Namespace declaration is removed from envelope and added to body.
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);**
前缀被删除因为原件没有任何前缀。
Prefix is removed as the original does not have any prefix.
这篇关于命名空间相关的错误,创建SOAP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!