Adding comment above root Node using DOM

J

Jay Sartoris

Hi,

I'm adding a node to my XML document using DOM. When I serialize it,
I lose my comments that are above my root node. I've created an
OutputFormat obect and set the setOmitComments(false) but it still
removes the comments between my DOCTYPE tag and my root tag. I'm
pretty sure that the reason is that only the text within the root tags
are serialized. My comments within the root tags are still there
after serialization, just the comment outside of the root tags are
removed.

Here is a snippet of my xml doc:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plugin SYSTEM "plugin.dtd">
<!--
// my comment here
-->

<plugin id="plugin1">
..
..
..
</plugin>

Does anyone have any idea what I'm doing wrong?

Thanks in advance,
Jay
 
T

Tjerk Wolterink

Jay said:
Hi,

I'm adding a node to my XML document using DOM. When I serialize it,
I lose my comments that are above my root node. I've created an
OutputFormat obect and set the setOmitComments(false) but it still
removes the comments between my DOCTYPE tag and my root tag. I'm
pretty sure that the reason is that only the text within the root tags
are serialized. My comments within the root tags are still there
after serialization, just the comment outside of the root tags are
removed.

Here is a snippet of my xml doc:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plugin SYSTEM "plugin.dtd">
<!--
// my comment here
-->

<plugin id="plugin1">
.
.
.
</plugin>

Does anyone have any idea what I'm doing wrong?

Thanks in advance,
Jay

You are doing it alright.
The XML Specifications says:

--
[Definition: Comments may appear anywhere in a document outside other
markup; in addition, they may appear within the document type
declaration at places allowed by the grammar. They are not part of the
document's character data; an XML processor may, but need not, make it
possible for an application to retrieve the text of comments. For
compatibility, the string "--" (double-hyphen) must not occur within
comments.] Parameter entity references are not recognized within comments.
--

Note the following sentence:
an XML processor may, but need not, make it possible for an application
to retrieve the text of comments.

So your DOM-library does not support retrieving comments outside the
root node i think. And that is alright (may, but need not), maybe you
should contact the author of the dom-package, and ask for this feature
you want.
 

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,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top