Greetings!

Is there any way to specify where namespace declarations appear in a serialized XML document via the DOM API? The problem is that if I construct document where root element contains child elements from another namespace, namespace declarations are repeated for each child element like this:

<ns1:root xmlns:ns1="urn:ns1">
  <ns2:child xmlns:ns2="urn:ns2"/>
  <ns2:child xmlns:ns2="urn:ns2"/>
</ns1:root>

This makes resulting XML look ugly. Especially, when embedded XML signature block (10+ elements) is appended to the root element as a child. I would like to specify all namespace declarations in the top-level element, like this:

<ns1:root xmlns:ns1="urn:ns1" xmlns:ns2="urn:ns2">
  <ns2:child/>
  <ns2:child/>
</ns1:root>

I use following code to construct and serialize document given above:

---8<-------------------------------------
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

Document document = builder.newDocument();
Element root = document.createElementNS( "urn:ns1", "ns:root" );
document.appendChild( root );

root.appendChild( document.createElementNS( "urn:ns2", "ns2:child" ) );
root.appendChild( document.createElementNS( "urn:ns2", "ns2:child" ) );

// now serialize the document using DOM level 3 capabilities
DOMImplementationRegistry registry =
    DOMImplementationRegistry.newInstance();

DOMImplementationLS impl =
    ( DOMImplementationLS ) registry.getDOMImplementation( "LS" );

LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter( "format-pretty-print", true );
LSOutput output = impl.createLSOutput();
output.setByteStream( System.out );
writer.write( document, output );
--->8-------------------------------------

Thanks in advance!

--
Igor Lobanov
Internal Development Engineer
SWsoft, Inc.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to