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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4037125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:48:54  来源:igfitidea点击:

NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces

javaxmlwebservice-clientxalansaaj

提问by Java Guy

Trying to retrieve the SOAP body from a SOAP response, but getting this error:

尝试从 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);

Using Saaj1.4

使用 Saaj1.4

What would be a fix for this?

有什么办法可以解决这个问题?

回答by Brent Matzelle

I had this exact problem myself and wasted a good half day on fixing it because of how vague the error message is. The problem is with your SOAP service (NOT the client implementation). It's throwing an error because there is a namespace issue with the XML you are sending to the client.

我自己也遇到了这个确切的问题,并且由于错误消息的模糊程度而浪费了半天的时间来修复它。问题在于您的 SOAP 服务(不是客户端实现)。它引发错误,因为您发送给客户端的 XML 存在命名空间问题。

There are three possible reasons for the issue according to this article:

根据本文,出现问题的可能原因有以下三种:

  1. A null namespace prefix
  2. A namespace prefix of "xml" that is not in the namespaceURI of "http://www.w3.org/XML/1998/namespace"
  3. A namespace prefix of "xmlns" that is not in the namespaceURI of "http://www.w3.org/2000/xmlns/"
  1. 空命名空间前缀
  2. 不在“http://www.w3.org/XML/1998/namespace”的namespaceURI中的“xml”命名空间前缀
  3. 不在“http://www.w3.org/2000/xmlns/”的namespaceURI中的“xmlns”的命名空间前缀

In my case it was #1 above that caused the problem. I wasn't returning the XML with a namespace. I fixed it by adding a namespace (the "ns" variable) to the root element and all child nodes like so:

就我而言,是上面的 #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));

It's important to note that my example code is for JDom, not Dom4jas the person was asking. You'll have to use the code appropriate for the XML library you happen to be using.

重要的是要注意,我的示例代码是针对JDom 的,而不是人们要求的Dom4j。您必须使用适合您碰巧使用的 XML 库的代码。

回答by jddsantaella

I faced the same problem. In my case, fix the problem on server side was not an option. I fixed it on client side forcing Xalan to version 2.7.0. See this.

我遇到了同样的问题。就我而言,在服务器端解决问题不是一种选择。我在客户端修复了它,迫使 Xalan 升级到 2.7.0 版。看到这个

回答by theINtoy

I had a similar issue with Flying Saucer. Following the advice from jddsantaella, I looked through my POM dependencies. The project I was using used Struts and under the covers struts had a dependency on Xalan 2.5.1.

我对飞碟也有类似的问题。按照 jddsantaella 的建议,我查看了我的 POM 依赖项。我使用的项目使用了 Struts,并且在幕后,struts 依赖于 Xalan 2.5.1。

I added the following to the POM in the struts dependency section:

我在 struts 依赖项部分的 POM 中添加了以下内容:

<exclusions>
<exclusion>
    <artifactId>xalan</artifactId>
        <groupId>xalan</groupId>
</exclusion>
</exclusions>

Flying Saucer now works a treat.

飞碟现在是一种享受。

Hope this helps.

希望这可以帮助。

回答by powerMicha

I had the same problem using spring-ws

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

By adding another third party library, xalan-2.6.0.jarwas added to my war file. This caused the same NAMESPACE_ERR

通过添加另一个第三方库,xalan-2.6.0.jar被添加到我的War文件中。这引起了同样的NAMESPACE_ERR

I resolved the error by adding xalan-2.7.0.jarinstead, as suggested by spring.

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

回答by Julius

I solved this by making the DocumentBuilderFactory namespace aware:

我通过让 DocumentBuilderFactory 命名空间知道来解决这个问题:

DocumentBuilderFactory.setNamespaceAware(true)