Jav*_*Guy 18 java xml webservice-client xalan saaj
尝试从SOAP响应中检索SOAP正文,但是收到此错误:
NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
Document doc = soapResMsg.getSOAPBody().extractContentAsDocument(); -- Exception is thrown here
org.dom4j.io.DOMReader d4Reader = new org.dom4j.io.DOMReader();
org.dom4j.Document d4doc = d4Reader.read(doc);
Run Code Online (Sandbox Code Playgroud)
使用Saaj1.4
对此有什么解决方法?
the*_*toy 10
我和Flying Saucer有类似的问题.根据jddsantaella的建议,我查看了我的POM依赖项.我使用的项目使用Struts,而struts下的项目依赖于Xalan 2.5.1.
我在struts依赖项部分的POM中添加了以下内容:
<exclusions>
<exclusion>
<artifactId>xalan</artifactId>
<groupId>xalan</groupId>
</exclusion>
</exclusions>
Run Code Online (Sandbox Code Playgroud)
飞碟现在是一种享受.
希望这可以帮助.
Jul*_*ius 10
我通过使DocumentBuilderFactory命名空间知道来解决了这个问题:
DocumentBuilderFactory.setNamespaceAware(true)
Run Code Online (Sandbox Code Playgroud)
我自己有这个确切的问题,因为错误信息有多模糊,所以浪费了半天修复它.问题出在您的SOAP服务上(而不是客户端实现).它正在抛出一个错误,因为您发送给客户端的XML存在名称空间问题.
在我的情况下,它是#1以上导致问题.我没有使用命名空间返回XML.我通过向根元素和所有子节点添加命名空间("ns"变量)来修复它,如下所示:
Namespace ns = Namespace.getNamespace("tns", "http://mycompany.com/schemas");
Element result = new Element("ResponseType", ns);
Document doc = new Document(result);
result.addContent(new Element("StatusCode", ns).setText(code));
result.addContent(new Element("Message", ns).setText(message));
Run Code Online (Sandbox Code Playgroud)
重要的是要注意我的示例代码是针对JDom的,而不是人们所问的Dom4j.您必须使用适合您正在使用的XML库的代码.