title: How to get wellformed XML of DOM document in Javascript?
body: <p>I want to serialize DOM in Javascript and expect to get a well-formed XML document, however <code>meta</code> tag is not closed:</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;meta name="keywords" content="test"&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;p&gt;test
      &lt;pre id='x'/&gt;
    &lt;/body&gt;
    &lt;script&gt;
      document.getElementById('x').appendChild(
        document.createTextNode(new XMLSerializer().serializeToString(document))
      );
    &lt;/script&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The output contains an invalid XML, where <code>meta</code> is not closed. However <code>p</code> is closed:</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;meta name="keywords" content="test"&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;p&gt;test&lt;/p&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The same behavior in Chrome 30.0 and Phantomjs 1.9.2.</p>
  
  <p>I'm expecting a well-formed XML output from <code>XMLSerializer</code>, even though my HTML input is broken. Why this is not happening and what is a workaround? I need to convert DOM to XML as a text.</p>