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

6

23

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

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

What would be a fix for this?

Straw answered 27/10, 2010 at 19:53 Comment(0)
S
16

I solved this by making the DocumentBuilderFactory namespace aware:

DocumentBuilderFactory.setNamespaceAware(true)
Snowflake answered 8/1, 2017 at 22:59 Comment(0)
C
12

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.

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/"

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:

  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 Dom4j as the person was asking. You'll have to use the code appropriate for the XML library you happen to be using.

Craunch answered 4/11, 2010 at 14:49 Comment(0)
A
12

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.

Archeology answered 27/1, 2012 at 16:56 Comment(1)
Thx! In my case the update was helpful, but when deploying on apache tomcat the problem appeared again. This was the result of a fat-jar including an older version of Xalan. The solution was to remove those class files from that legacy jar. Maybe this hint is helpful to some people.Laird
N
10

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.

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

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

Flying Saucer now works a treat.

Hope this helps.

Nonprofit answered 15/6, 2012 at 12:30 Comment(0)
G
10

I had the same problem using spring-ws

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

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

Glomma answered 12/7, 2012 at 14:33 Comment(2)
What do i have to do if i'm forced to use xalan-2.6.0 by another dependency?Tableland
Thanks, this worked for me. Upgrading this library fixed my issue and didn't seem to affect the code that uses that library.Smirk
C
0

I encountered the issue with Java 8 and WebLogic 12c.

In my case, it helped to add to WAR org.glassfish.jaxb:jaxb-runtime:2.3.3

The tricky thing was that the application worked without the dependency pretty long time. But something in one particular request caused this error and I am still not sure what it was. However, the new dependency helped.

Calix answered 20/11, 2020 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.