我试图构建一个可以接受任何类型的请求文档(带或不带命名空间)调用 web 服务的 web 服务客户端。但我面临着下面提到的。谁能帮我解决这个问题。我知道这是他们要求将文档工厂的“setNamespaceAware”设置为 true 的类似问题的解决方案
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)