<p>I wrote a simple tool to generate a DBUnit XML dataset using queries that the user enters.  I want to include each query entered in the XML as a comment, but the DBUnit API to generate the XML file doesn't support inserting the comment where I would like it (above the data it generates), so I am resorting to putting the comment with ALL queries either at the top or bottom.</p>

<p>So my question: is it valid XML to place it at either location?  For example, above the XML Declaration:</p>

<pre><code>&lt;!-- Queries used: ... --&gt;
&lt;?xml version='1.0' encoding='UTF-8'?&gt;
&lt;dataset&gt;
  ...
&lt;/dataset&gt;
</code></pre>

<p>Or below the root node:</p>

<pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
&lt;dataset&gt;
  ...
&lt;/dataset&gt;
&lt;!-- Queries used: ... --&gt;
</code></pre>

<p>I plan to initially try above the XML Declaration, but I have doubts on if that is valid XML, despite the claim from <a href="http://en.wikipedia.org/wiki/Xml#Well-formed_documents:_XML_syntax" rel="noreferrer">wikipedia</a>:</p>

<blockquote>
  <p>Comments can be placed anywhere in the tree, including in the text if the content of the element is text or #PCDATA.</p>
</blockquote>

<p>I plan to post back if this works, but it would be nice to know if it is an official XML standard.</p>

<p><strong>UPDATE:</strong> See <a href="https://stackoverflow.com/questions/50824/can-xml-comments-go-anywhere#50976">my response below</a> for the result of my test.</p>

[{'answer_id': 50832, 'author': 'Anonymoose', 'author_id': 2391, 'author_profile': 'https://Stackoverflow.com/users/2391', 'pm_score': 5, 'selected': True, 'text': '<p>According to the <a href="http://www.w3.org/TR/2006/REC-xml-20060816/#sec-comments" rel="noreferrer">XML specification</a>, a well-formed XML document is:</p>\n\n<blockquote>\n  <p><code>document    ::=       prolog  element  Misc*</code></p>\n</blockquote>\n\n<p>where <code>prolog</code> is</p>\n\n<blockquote>\n  <p><code>prolog      ::=       XMLDecl? Misc* (doctypedecl  Misc*)?</code></p>\n</blockquote>\n\n<p>and <code>Misc</code> is</p>\n\n<blockquote>\n  <p><code>Misc        ::=       Comment | PI | S</code></p>\n</blockquote>\n\n<p>and</p>\n\n<blockquote>\n  <p><code>XMLDecl     ::=      \'&lt;?xml\' VersionInfo  EncodingDecl? SDDecl? S? \'?&gt;\'</code></p>\n</blockquote>\n\n<p>which means that, if you want to have comments at the top, you cannot have an XML type declaration. </p>\n\n<p>You can, however, have comments after the declaration and outside the document element, either at the top or the bottom of the document, because <code>Misc*</code> can contain comments.</p>\n\n<p>The specification agrees with Wikipedia on comments:</p>\n\n<blockquote>\n  <p>2.5 Comments</p>\n  \n  <p>[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 MUST NOT be recognized within comments.</p>\n</blockquote>\n\n<p>All of this together means that you can put comments <strong>anywhere that\'s not inside other markup</strong>, except that you <strong>cannot have an XML declaration if you lead with a comment</strong>.</p>\n\n<p>However, while in theory theory agrees with practice, in practice it doesn\'t, so I\'d be curious to see how your experiment works out.</p>\n'}, {'answer_id': 50837, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 3, 'selected': False, 'text': '<p>The first example is not valid XML, the declaration has to be the first thing in a XML document.</p>\n\n<p>But besides that, comments can go anywhere else.</p>\n\n<p>Correcting your first example:</p>\n\n<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;\n&lt;!-- Queries used: ... --&gt;\n&lt;dataset&gt;\n&lt;/dataset&gt;\n</code></pre>\n'}, {'answer_id': 50850, 'author': 'David Schlosnagle', 'author_id': 1750, 'author_profile': 'https://Stackoverflow.com/users/1750', 'pm_score': 2, 'selected': False, 'text': '<p>The  processing instruction must be the very first thing in the XML content (see <a href="http://www.w3.org/TR/REC-xml/#sec-comments" rel="nofollow noreferrer">XML comment</a> and <a href="http://www.w3.org/TR/REC-xml/#sec-pi" rel="nofollow noreferrer">processing instructions</a>). The following should work:</p>\n\n<pre><code>&lt;?xml version=\'1.0\' encoding=\'UTF-8\'?&gt;\n&lt;!-- Queries used: ... --&gt;\n&lt;dataset&gt;\n  ...\n&lt;/dataset&gt;\n</code></pre>\n'}, {'answer_id': 50976, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 2, 'selected': False, 'text': '<p>Thanks for the answers everyone!</p>\n\n<p>As it turns out, the comment ahead of the file seemed to work, but when I delved into the DBUnit source, it is because validation is turned off.</p>\n\n<p>I did try a simple document load via:</p>\n\n<pre><code>DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();\nDocumentBuilder builder = factory.newDocumentBuilder();\nDocument document = builder.parse(new File("/path/to/file"));\n</code></pre>\n\n<p>and this fails with an exception because the XML Declaration is not the first thing (as others indicated would be the case).</p>\n\n<p>So, while DBUnit would work, I prefer to have valid XML, so I moved the comment to the end (since DBUnit generates the XML Declaration, it is not an option to place the comment below it, even though I would prefer that... at least not without modifying the XML after the fact, which would be more work than it is worth).</p>\n'}]
['https://Stackoverflow.com/questions/50824', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/122/']