title: How to validate XML using Nokogiri?
body: <p>I have to validate an XML document so it will not accept an invalid XML document.</p>
  
  <p>I did it this way to handle an invalid document:</p>
  
  <pre><code>xml ||= Nokogiri::XML xml_data do |config| 
    config.strict
  end
  rescue Nokogiri::XML::SyntaxError =&gt; e
    puts "caught exception: #{e}"
  else
    #further processing if no error
  </code></pre>
  
  <p>But even for the valid XML document, it shows:</p>
  
  <pre><code>caught exception: Extra content at the end of the document
  </code></pre>
  
  <p>Sample XML i'm using:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE note SYSTEM "Note.dtd"&gt;
  &lt;note&gt;
  &lt;to&gt;Tove&lt;/to&gt;
  &lt;from&gt;Jani&lt;/from&gt;
  &lt;heading&gt;Reminder&lt;/heading&gt;
  &lt;body&gt;Don't forget me this weekend!&lt;/body&gt;
  &lt;/note&gt;
  </code></pre>
  
  <p>What am I doing wrong?</p>