How does DOM model whitespace before the document element?

Hi all,

I failed to understand how DOM models whitespace before the document
element.

E.g. in the following fragment, the white space between the XML declaration
and the starting tag of the <root> element:

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

<root>
    <child />
</root>

When I parse such a document and get the DOM objects using DocumentBuilder
(in java), then later write the objects back to a file using Transformer,
the whitespace is preserved. But I cant see the object responsible for the
whitespace in the DOM object tree.

And also, when I try to insert a text element before the document element I
get an exception indicating "HIERARCHY_REQUEST_ERR: An attempt was made to
insert a node where it is not permitted." This is consistent with DOM level1
spec, as text nodes dont seem to

This means that I cant insert whitespace before the document element when
I'm programmatically creating the document, and my document becomes:

<?xml version="1.0" encoding="UTF-8"?><root>
    <child />
</root>

Though identical in semantics, it does not look very good and readable.

Any ideas or knowledge on this?

Thanks in advance,
guven

Received on Thursday, 20 March 2008 04:23:39 UTC