SOAPBody NAMESPACE_ERR
angusm Jul 4, 2006 4:42 AMIf I create a Document by reading it from a String (as in the WS examples) I can add it to a SOAPBody. But if I create the same document programatically I get a NAMESPACE_ERR.
In the example below I created the Document programatically, converted it to a String, and then back to a Document. The second worked, the first did not.
I have changed the version of the XerceImpl.jar, without any difference.
Any advice would be much appreciated. Should I submit a bug?
adding doc from ByteArrayInputStream: <?xml version="1.0" encoding="UTF-8"?> <Order><Customer>Kermit</Customer><Item>Ferrari</Item></Order> adding doc from program: 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 org.apache.xerces.dom.ElementNSImpl.setName(Unknown Source) at org.apache.xerces.dom.ElementNSImpl.<init>(Unknown Source) at org.apache.xerces.dom.CoreDocumentImpl.createElementNS(Unknown Source) at org.jboss.util.xml.DOMUtils.createElement(DOMUtils.java:140) at org.jboss.ws.soap.SOAPElementImpl.<init>(SOAPElementImpl.java:79) at org.jboss.ws.soap.SOAPFactoryImpl.createElement(SOAPFactoryImpl.java:75) at org.jboss.ws.soap.SOAPBodyImpl.addDocument(SOAPBodyImpl.java:82) at test.TestNs.<init>(TestNs.java:40) at test.TestNs.main(TestNs.java:73)
Program:
package test;
import java.io.ByteArrayInputStream; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; import org.w3c.dom.Document; import org.w3c.dom.Element; import com.ibm.wsdl.util.xml.DOM2Writer; public class TestNs { public TestNs() { try { MessageFactory mf = MessageFactory.newInstance(); SOAPBody body = mf.createMessage().getSOAPBody(); SOAPBody body2 = mf.createMessage().getSOAPBody(); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); Document doc = getDoc(); StringWriter swr = new StringWriter(); DOM2Writer.serializeAsXML(doc, swr); String xml = swr.toString(); Document doc2 = builder.parse(new ByteArrayInputStream(xml.getBytes())); System.out.println("adding doc from ByteArrayInputStream: \n" + swr); body2.addDocument(doc2); System.out.println("adding doc from program:\n"); // NAMESPACE_ERR ! body.addDocument(doc); } catch (Exception e) { e.printStackTrace(); } } private Document getDoc() { try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); Document root = builder.newDocument(); Element orderEl = root.createElement("Order"); root.appendChild(orderEl); Element customerEl = root.createElement("Customer"); orderEl.appendChild(customerEl); customerEl.appendChild(root.createTextNode("Kermit")); Element itemEl = root.createElement("Item"); orderEl.appendChild(itemEl); itemEl.appendChild(root.createTextNode("Ferrari")); return root; } catch (Exception e) { e.printStackTrace(); return null; } } public static void main(String[] args) { new TestNs(); } }