XmlHTTPRequest: "XML parsing error: element not found"

So, I use PHP + MySQL to deliver the contents of the database in XML to 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 the result, for example ...


<?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 am loading XML using JavaScript:


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 will stay right here. Because when I try to parse XML, I get the following error in Firebug:


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

I tried downloading ./leer/confesiones/by entering it as a URL into the browser and it works like a charm. Fully valid XML. Using Firebug to validate XHR in "Net" also indicates valid XML. The console view is the one that gives me an error, for example if it is a JavaScript error. But it http.responseTextcontains XML in the text and xmlhas a type [object XMLDocument].

So ... what am I missing?

SOLVED: PHP JSON JavaScript .

+3
4

JS-, ajax . gotchas, .

jQuery, . html :

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

JS - :

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

. jQuery $.get. BTW. , POST, ( ) GET.

, PHP, JSON XML. , , xml, :

echo json_encode($array); // voila
+4
if (typeof window.ActiveXObject != 'undefined' ) {

XMLHttpRequest. ActiveXObject , ActiveX.

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

. Rememeber var.

    http.onreadystatechange = oncomplete;

onreadystatechange , . oncomplete readystatechange readyState===4, XML.

    http.onload = oncomplete;

onload XMLHttpRequest. onreadystatechange .

+1

, JQuery ... , : XMLHttpRequest xml jQuery 1.4.1, ?

, , , - , XML " " , XML ( ).

+1

I do not know PHP, but I do not see where you get your closing tag for the element <confession>. Call me embarrassed as you say the result is well-formed ...

0
source

Source: https://habr.com/ru/post/1731649/


All Articles