1

i have to create xml something like:

<xml version="1.0" encoding="UTF-8"?>
<tns:Message>
  <tns:Header>
    <tns:to>CCM</tns:to>
    <tns:from>CPM</tns:from>
    <tns:type>New</tns:type>
  </tns:Header>
</tns:Message>

from my java object.

I am trying to do something like this

                     DocumentBuilderFactory factory 
               = DocumentBuilderFactory.newInstance();
              factory.setNamespaceAware(true);
              DocumentBuilder builder = factory.newDocumentBuilder();
              DOMImplementation impl = builder.getDOMImplementation();
              Document doc = impl.createDocument(null,"tns:Message", null);

but in the last line it gives me error

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

but if i pass "Message" instead of "tns:Message" it works fine. Since tns is the namespace prefix am i need to use it , how can i make it possible.

Any suggestions?

1 Answer 1

1

http://java.sun.com/j2se/1.4.2/docs/api/org/w3c/dom/DOMImplementation.html#createDocumentType%28java.lang.String,%20java.lang.String,%20java.lang.String%29

Look at the second method given for createDocument.

public Document createDocument(String namespaceURI,
                           String qualifiedName,
                           DocumentType doctype)
                    throws DOMException

you need to provide a uri to uniquely identify the namespace as the first parameter

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.