tags:

views:

504

answers:

1

Hi, 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  A: 

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

Jonathan Fingland
it worked thanks
harshit