title: XMLWriter error when creating XML file using PHP
body: <p>After executing the code below (minus the database call), I am receiving a "error on line 331 at column 7: Extra content at the end of the document" error. I went through these forums, but could not find a solution. I don't have any random characters or any code that should be adding that extra whitespace... Any ideas?</p>
  
  <pre><code>&lt;?php 
  header('Content-type: text/xml');
  mysql_connect("localhost", "---", "---");
  mysql_select_db("---");
  
  $query = "SELECT title FROM table";
  $result = mysql_query($query);
  
  $xml = new XMLWriter();
  $xml-&gt;openURI("php://output");
  $xml-&gt;startDocument();
  $xml-&gt;setIndent(true);
  $xml-&gt;writeRaw('&lt;urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"&gt;');
  $xml-&gt;startElement('url');
  while ($row = mysql_fetch_assoc($result)) {
      if(!empty($row)){
          $title = $row['title'];
          $xml-&gt;startElement('loc');
              $xml-&gt;writeRaw('http://domain.com/article/');
          $xml-&gt;endElement();
          $xml-&gt;startElement('news:news');
              $xml-&gt;startElement("news:publication");
                  $xml-&gt;startElement("news:name");
                      $xml-&gt;writeRaw('Name');
                  $xml-&gt;endElement();
                  $xml-&gt;startElement("news:language");
                      $xml-&gt;writeRaw('en');
                  $xml-&gt;endElement();
              $xml-&gt;endElement();
              $xml-&gt;startElement('news:title');
                  $xml-&gt;writeRaw($title);
              $xml-&gt;endElement();
              $xml-&gt;endElement();
      }
  }
  $xml-&gt;endElement();
  $xml-&gt;flush();
  </code></pre>