NAMESPACE_ERR:尝试以对名称空间不正确的方式创建或更改对象

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

对此有什么解决方法?

jdd*_*lla 11

我遇到了同样的问题.在我的情况下,修复服务器端的问题不是一个选项.我在客户端修复它,强制Xalan到2.7.0版本.看到这个.


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)


Bre*_*lle 9

我自己有这个确切的问题,因为错误信息有多模糊,所以浪费了半天修复它.问题出在您的SOAP服务上(而不是客户端实现).它正在抛出一个错误,因为您发送给客户端的XML存在名称空间问题.

根据这篇文章,这个问题有三个可能的原因:

  1. 空名称空间前缀
  2. "xml"的名称空间前缀,不在"http://www.w3.org/XML/1998/namespace"的namespaceURI中
  3. "xmlns"的名称空间前缀不在"http://www.w3.org/2000/xmlns/"的namespaceURI中

在我的情况下,它是#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库的代码.


pow*_*cha 8

我使用spring-ws遇到了同样的问题

通过添加另一个第三方库,xalan-2.6.0.jar添加到我的war文件中.这也是一样的NAMESPACE_ERR

xalan-2.7.0.jar按照spring的建议添加了错误来解决错误.