title: Node.js - XML validation
body: <p>How do you validate xml in node.js? I'm mainly interested in checking only if the file is well formed. Additionally I want to receive a line number where error occurs.</p>
  <p>I have the following test xml:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
  &lt;body&gt;
      &lt;tagname&gt;
          test1
      &lt;/tag.name&gt;   &lt;--- error here
      &lt;tagname&gt;
          test2
      &lt;/tagname&gt;
  &lt;/body&gt;
  </code></pre>
  <p>What I have tried so far:</p>
  <p>xmldom:</p>
  <pre><code>var parser = new DOMParser({errorHandler: (level, error) =&gt; {
      console.log (level, error);
  }});
  var dom = parser.parseFromString(content, 'application/xml');
  </code></pre>
  <p>Problem: There is no indication of error. Error handler is never called, and dom contains &quot;test1&quot; but not &quot;test2&quot;. So parser just stops and swallows the error.</p>
  <p>libxmljs:</p>
  <pre><code>var xmlDoc = libxmljs.parseXml(xml);
  </code></pre>
  <p>Problem: throws undescriptive exception &quot;Premature end of data in tag body&quot;.</p>