title: XmlHTTPRequest: "XML Parsing Error: no element found"
body: <p>So I'm using PHP+MySQL to deliver database contents in XML to a JavaScript.</p>
  
  <p><pre><code>
  $xml = "&lt;?xml version='1.0' encoding='utf-8'?>&lt;confessions>&lt;pending>";
  $pending = $m->MySqlHandler->Query("SELECT id, gender, age, confession, date_posted FROM confessions WHERE publish = 0");
  foreach ($pending->Rows as $pr)
  {
     list($id, $gender, $age, $confession, $dateposted) = array(
       $pr->Columns["id"]->Value,
       $pr->Columns["gender"]->Value,
       $pr->Columns["age"]->Value,
       $pr->Columns["confession"]->Value,
       $pr->Columns["date_posted"]->Value
     );
     $xml .= "&lt;confession id='$id' gender='$gender' age='$age' dateposted='$dateposted'>$confession&lt;/confession>";
  }</p>
  
  <p>$xml .= "&lt;/pending>";
  $xml .= "&lt;active>";</p>
  
  <p>$active = $m->MySqlHandler->Query(
      "SELECT DISTINCT confessions.*,
      (SELECT COUNT(*) FROM comments WHERE confession_id = confessions.id) AS comments,
      (SELECT COUNT(*) FROM sentences WHERE confession_id = confessions.id) AS sentences
      FROM confessions WHERE confessions.publish = 1"
  );</p>
  
  <p>foreach ($active->Rows as $ar)
  {
      list($id, $gender, $age, $confession, $dateposted, $absolutions) = array(
           $ar->Columns["id"]->Value,
           $ar->Columns["gender"]->Value,
           $ar->Columns["age"]->Value,
           $ar->Columns["confession"]->Value,
           $ar->Columns["dateposted"]->Value,
           $ar->Columns["absolutions"]->Value
      );
      $sql_template = "SELECT COUNT(*) FROM sentences WHERE confession_id = $id AND degree = '%s'";
      $sentence_data = array(
         "t" => mysql_result(mysql_query(sprintf($sql_template, "t")), 0, 0),
         "c" => mysql_result(mysql_query(sprintf($sql_template, "c")), 0, 0),
         "p" => mysql_result(mysql_query(sprintf($sql_template, "p")), 0, 0),
         "l" => mysql_result(mysql_query(sprintf($sql_template, "l")), 0, 0)
      );
      $xml .= "&lt;confession absolutions='$absolutions' t='{$sentence_data['t']}' "
           . "c='{$sentence_data['c']}' p='{$sentence_data['p']}' "
           . "l='{$sentence_data['l']}' id='$id' gender='$gender' "
           . "age='$age'>$confession&lt;/confession>";
  }</p>
  
  <p>$xml .= "";
  header("Content-Type: application/xml");
  echo $xml;</p>
  
  <p></pre></code></p>
  
  <p>So, from there, you get a result such as...</p>
  
  <p><pre><code>
  &lt;?xml version='1.0' encoding='utf-8'?>
  &lt;confessions>
    &lt;pending>
      &lt;confession id="1" gender="m" age="20" dateposted="2010-02-06 05:22:57">
        Confesando.
      &lt;/confession>
    &lt;/pending>
    &lt;active>
      &lt;confession absolutions="0" t="0" c="0" p="0" l="0" id="2" gender="m" age="18">
        Confesion.
      &lt;/confession>
    &lt;/active>
  &lt;/confessions></p>
  
  <p></pre></code></p>
  
  <p>I load the XML with JavaScript by using:</p>
  
  <pre><code>
  function sendData(params, to, oncomplete)
  {
      if (typeof oncomplete == 'undefined')
      {
          oncomplete = function() {};
      }
      if (typeof window.ActiveXObject != 'undefined' ) {
          http = new ActiveXObject("Microsoft.XMLHTTP");
          http.onreadystatechange = oncomplete;
      } else {
          http = new XMLHttpRequest();
          http.onload = oncomplete;
      }
      http.open("POST", to, false);
      http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      http.setRequestHeader("Content-Length", params.length);
      http.setRequestHeader("Connection", "close");
      http.send(params);
  }
  </code></pre>
  
  <p>...which is called like this:</p>
  
  <p><pre><code>
  // Load approval-pending data //
  sendData("", "./leer/confesiones/" + sId, function()
  {
     var xml = http.responseXML;
     var pending = xml.getElementsByTagName("pending").getElementsByTagName("confession");
  (...)
  </pre></code></p>
  
  <p>I'll stop right here. Because when I attempt to parse the XML I get the following error at Firebug:</p>
  
  <p><pre><code>
  XML Parsing Error: no element found Location: moz-nullprincipal:{7e9eab45-2f73-476d-9bdb-2370d1534f29} Line Number 1, Column 1:
  ^
  </pre></code></p>
  
  <p>I tried loading <code>./leer/confesiones/</code> by inputting it as an URL into the browser and it works like a charm. Fully valid XML. Using Firebug to inspect XHR under "Net" says so too, valid XML. The Console view is the one that gives me the error, like if it is a JavaScript error. But <code>http.responseText</code> contains the XML, in text, and <code>xml</code> is of type <code>[object XMLDocument]</code>.</p>
  
  <p>So... what am I missing?</p>
  
  <p><strong>SOLVED</strong>: modified PHP to output JSON and JavaScript to parse it properly.</p>