XmlHTTPRequest: "XML Parsing Error: no element found"

22,542

Solution 1

Do yourself a favor, and use a JS library that wraps all the ajax magic for you. There's a lot of cross-browser issues and gotchas, and this may just be one of those things.

I'd recommend jQuery, it's the easiest and quite powerful. So add this to the top of your html, inside the head tag:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> 

And then in your JS do something like this:

 $.get('/leer/confesiones/', function(data) {
     console.log(data);
 });

That should get you started. Look here for more info about jQuery and the $.get function. BTW- I see you're using a POST, but for retrieval of data (no updating or deleting) the convention is GET.

Additionally, consider changing your PHP so that it returns JSON formatted data instead of XML. So instead of doing that little dance you have to do with the xml markup, just get an array ready with all the data, and do this:

echo json_encode($array); // voila

Solution 2

I'm a bit surprised if JQuery solved this particular issue... at least, you can generate the same errors with that: XMLHttpRequest xml response woes with jQuery 1.4.1, how to force the request response to be processed as plain text?

In my case, at least, this was all due to the fact that I was making a cross-domain request, and the XML error about it being "unparsable" was just a side effect of the fact that no XML came back (because the browser didn't allow it).

Solution 3

if (typeof window.ActiveXObject != 'undefined' ) {

I'd go for native XMLHttpRequest first. ActiveXObject should only be a fallback, as it may generate ugly ActiveX warnings.

    http = new ActiveXObject("Microsoft.XMLHTTP");

Accidental global. Rememeber to use var.

    http.onreadystatechange = oncomplete;

onreadystatechange is called in more cases than just completion. You should only call oncomplete when readystatechange fires and readyState===4, otherwise you may be attempting to parse incomplete or missing XML.

    http.onload = oncomplete;

onload is not part of the XMLHttpRequest standard. You should use onreadystatechange for both branches.

Share:
22,542
Joel A. Villarreal Bertoldi
Author by

Joel A. Villarreal Bertoldi

Web Designer/Developer, art lover.

Updated on January 27, 2020

Comments

  • Joel A. Villarreal Bertoldi
    Joel A. Villarreal Bertoldi over 4 years

    So I'm using PHP+MySQL to deliver database contents in XML to a JavaScript.

    
    $xml = "<?xml version='1.0' encoding='utf-8'?><confessions><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 .= "<confession id='$id' gender='$gender' age='$age' dateposted='$dateposted'>$confession</confession>";
    }

    $xml .= "</pending>"; $xml .= "<active>";

    $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" );

    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 .= "<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</confession>"; }

    $xml .= ""; header("Content-Type: application/xml"); echo $xml;

    So, from there, you get a result such as...

    
    <?xml version='1.0' encoding='utf-8'?>
    <confessions>
      <pending>
        <confession id="1" gender="m" age="20" dateposted="2010-02-06 05:22:57">
          Confesando.
        </confession>
      </pending>
      <active>
        <confession absolutions="0" t="0" c="0" p="0" l="0" id="2" gender="m" age="18">
          Confesion.
        </confession>
      </active>
    </confessions>

    I load the XML with JavaScript by using:

    
    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);
    }
    

    ...which is called like this:

    
    // Load approval-pending data //
    sendData("", "./leer/confesiones/" + sId, function()
    {
       var xml = http.responseXML;
       var pending = xml.getElementsByTagName("pending").getElementsByTagName("confession");
    (...)
    

    I'll stop right here. Because when I attempt to parse the XML I get the following error at Firebug:

    
    XML Parsing Error: no element found Location: moz-nullprincipal:{7e9eab45-2f73-476d-9bdb-2370d1534f29} Line Number 1, Column 1:
    ^
    

    I tried loading ./leer/confesiones/ 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 http.responseText contains the XML, in text, and xml is of type [object XMLDocument].

    So... what am I missing?

    SOLVED: modified PHP to output JSON and JavaScript to parse it properly.

  • Joel A. Villarreal Bertoldi
    Joel A. Villarreal Bertoldi over 14 years
    Element goes like this: <confession [attributes]>..text-goes-here..</confession>
  • BobS
    BobS over 14 years
    My point is, which part of the PHP code creates the closing <confession> tag?
  • Joel A. Villarreal Bertoldi
    Joel A. Villarreal Bertoldi over 14 years
    Sorry 'bout that. Forgot to type some <'s as &lt;
  • Joel A. Villarreal Bertoldi
    Joel A. Villarreal Bertoldi over 14 years
    That worked like a charm. Actually, I was already using it somewhere else in my web application. I don't know why I thought it'd be a good idea to send XML as response instead of JSON. Thanks a lot!
  • Eugen
    Eugen almost 13 years
    That is all well and good, but was there a conclusion regarding the underlying issue (XML Parsing Error: no element found Location: moz-nullprincipal:{7e9eab45-2f73-476d-9bdb-2370d1534f29} Line Number 1, Column 1:)? Thanks.