Setting namespaces in DOM

R

Rogan Dawes

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top