Setting namespaces in DOM  
Author Message
Rogan Dawes





PostPosted: 2005-9-30 23:22:00 Top

java-programmer, Setting namespaces in DOM Hi all,

I'm trying to construct an XML (SOAP) document, with a number of namespaces.

DOM provides Document.createElementNS(), which can be used to add the
namespace in the particular element. However, I often get the case where
the namespace definition is repeated in multiple sibling elements, while
it would be a lot simpler (cleaner?) to define the namespace only once
at a higher level.

I tried using

document.getRootElement().setAttribute("xmlns:xsd", XSD_NS);

but, even though the document.toString() shows the correct xmlns:xsd
attribute in the root node, it is still repeated in the lower level
elements.

Does anyone have any idea of how the DOM decides when to "roll-up" a
namespace definition to a higher level? Or how to tell it to?

I'm talking about the difference between:

<rootnode>
<childnode xsi:type="xsd:int" xmlns:xsi="http://w3c.blah"/>
<childnode xsi:type="xsd:int" xmlns:xsi="http://w3c.blah"/>
<childnode xsi:type="xsd:int" xmlns:xsi="http://w3c.blah"/>
</rootnode>

and

<rootnode xmlns:xsi="http://w3c.blah">
<childnode xsi:type="xsd:int"/>
<childnode xsi:type="xsd:int"/>
<childnode xsi:type="xsd:int"/>
</rootnode>

Many thanks.

Rogan