By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,529 Members | 864 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,529 IT Pros & Developers. It's quick & easy.

How to declare namespace prefix in Java?

P: n/a
I want to declare namespace prefix in the Envelope element (i.e.
xmlns:xsi and xmlns:xsd) so that the document will be serialized to
look like the following. However, I don't know the right way to do
that in Java.

<?xml version='1.0' encoding='UTF-8'?>

<key xsi:type='xsd:string'>mykey</key>
<phrase xsi:type='xsd:string'>word2check</phrase>
Currently, my program is as follows:

Document doc =
Element root = doc.getDocumentElement();
/* I doubt this is the right way to declare namespace prefix */
root.setAttribute("xmlns:xsd", "");

Element body =

Element doSpellingSuggestion = doc.createElementNS("urn:GoogleSearch",

Element key = doc.createElement("key");
/ * well, i can use setAttribute() to get rid of the extra namespace in
the output, but is there a better way?*/
"xsi:type", "xsd:string");

And the output looks like

How do I get rid of the superfluous namespace thingy in the <key> and
<phrase> element? Thanks a lot!

Mar 14 '06 #1
Share this Question
Share on Google+
2 Replies

P: n/a
If you're working with the DOM, there are two answers to this.

One is that, in many cases, it isn't strictly necessary to have an
actual namespace declaration. Use the proper createElementNS and
createAttributeNS directives, and the code which serializes the DOM out
to XML syntax should be able to create delarations automatically. See
the DOM level 3 working draft for a description of the algorithms
typically used to do namespace reconcilliation in the DOM.

On the other hand, some programs want an explicit namespace declaration
in the DOM for internal purposes, and creating one at a higher level of
the document will avoid potentially having to generate one at multiple
places lower down. So the DOM permits you to do this; the trick is to
use createAttributeNS or setAttributeNS to create an attribute in the
"namespace for namespaces" with the prefix xmlns:, the localname set to
the prefix you want to define, and the value being the URI you want to
bind that prefix to. Thus, uour example should be coded as:


Important Note: If you're working with namespaces, you ABSOLUTELY MUST
NOT use the setAttribute, createAttribute, or createElement calls.
****ONLY**** the setAttributeNS, createAttributeNS, and createElementNS
versions are compatable with namespace-aware processing. (There are good
but ugly reasons why we couldn't deprecate the older calls, but they
should be considered obsolete except for backward compatability with
code that is intended to work with a Level 1 DOM.)

See DOM Level 2's description of how to work with namespaces; a copy can
be found at
How do I get rid of the superfluous namespace thingy in the <key> and
<phrase> element? Thanks a lot!

If the above changes don't resolve this for you, it's possible the
serializer you're using is broken...

() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Mar 15 '06 #2

P: n/a
Thanks Joe. You rock!!!

Mar 15 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.