我正在尝试构建一个Web服务客户端,它可以接受任何类型的请求文档(带或不带名称空间)调用webservice。但我面对下面提到的。任何人都可以帮我这个。我知道这是一个类似问题的解决方案,他们要求设置“setNamespaceAware'对文档工厂来说是真的
factorydocFactory = DocumentBuilderFactory.newInstance();
docFactory.setNamespaceAware(true);
但在我的情况下,请求文档来自其他地方,我无法控制它。一旦我收到文档,我需要做一些使命名空间被忽略但我不知道如何做的事情。
代码:
public SOAPMessage createSoapRequest(Document requestDoc)
throws SOAPException, DOMException, ParserConfigurationException, SAXException, IOException {
System.setProperty("javax.xml.soap.MessageFactory", "com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl");
MessageFactory mf = SOAPMessageFactory1_1Impl.newInstance();
SOAPMessage request = mf.createMessage();
SOAPPart soapPart = request.getSOAPPart();
SOAPEnvelope env = soapPart.getEnvelope();
SOAPBody soapBody = env.getBody();
/* adding the input to soapbody */
soapBody.addDocument(requestDoc);
return request;
}
输入传递:
<tem:abc xmlns:tem="http://tempuri.org/">
<tem:def>
<MESSAGE>
<Component>
<Parameters>
<adsfds>123 </adsfds>
</Parameters>
</Component>
</MESSAGE>
</tem:def>
</tem:abc>
我得到例外:
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.
at com.sun.org.apache.xerces.internal.dom.ElementNSImpl.setName(Unknown Source)
at com.sun.org.apache.xerces.internal.dom.ElementNSImpl.<init>(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl.<init>(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.ElementFactory.createElement(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl.createElement(Unknown Source)
at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.importNode(Unknown Source)
at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.importNode(Unknown Source)
at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.importNode(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl.importNode(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.BodyImpl.addD`enter code heredocument(Unknown Source)
at com.custom.util.TestWebServiceCaller.createSoapRequest(TestWebServiceCaller.java:224)
at com.custom.util.TestWebServiceCaller.invokeWS(TestWebServiceCaller.java:103)
at TestWS.main(TestWS.java:30)