Failed to shutdown with CData

When I use DOMDocument :: loadXML () for my XML below, I get the error:

Warning: DOMDocument::loadXML() [domdocument.loadxml]: CData section not finished http://www.site.org/displayimage.php?album=se in Entity,
Warning: DOMDocument::loadXML() [domdocument.loadxml]: Premature end of data in tag image line 7 in Entity
Warning: DOMDocument::loadXML() [domdocument.loadxml]: Premature end of data in tag quizz line 3 in Entity
Warning: DOMDocument::loadXML() [domdocument.loadxml]: Premature end of data in tag quizzes line 2 in Entity
Fatal error: Call to a member function getElementsByTagName() on a non-object 

      

It seems to me that my CData partitions are closed, but still I get this error. XML looks like this:

<?xml version="1.0" encoding="utf-8"?>
<quizzes>
<quizz>
<title><![CDATA[Title]]></title>
<descr><![CDATA[Some text here!]]></descr>
<tags><![CDATA[one tag, second tag]]></tags>
<image><![CDATA[http://www.site.org/displayimage.php?album=search&cat=0&pos=1]]></image>
<results>
<result>
<title><![CDATA[Something]]></title>
<descr><![CDATA[Some text here]]></descr>
<image><![CDATA[http://www.site.org/displayimage.php?album=search&cat=0&pos=17]]></image>
<id>1</id>
</result>
</results>
</quizz>
</quizzes>

      

Could you help me figure out what is the problem?

+2


a source to share


5 answers


I found that the problem was passing this XML to PHP using cURL. I sent it as plain text and the char in this XML was interpreted as a delimiter to the next parameter. So when I eluded this char it started working fine.



-1


a source


I found that XML hidden characters usually have problems, so I prefer to avoid invalid characters like favorites:



<?php
//$feedXml is the fetched XML content
$invalid_characters = '/[^\x9\xa\x20-\xD7FF\xE000-\xFFFD]/';
$feedXml = preg_replace($invalid_characters, '', $feedXml );

      

+4


a source


Sorry if this is disabled because this is only related to a specific PHP case when using cURL, but as with tomaszs states, I also found that ampersands can cause a problem when passing XML over cURL to PHP. I was getting a known valid XML string with properly encoded ampersands and then redirecting it to another cURL address. Something like that...

$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL,            $fullUri);
curl_setopt($curlHandle, CURLOPT_HEADER,         false);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_CONNECTTIMEOUT, 4); // seconds
curl_setopt($curlHandle, CURLOPT_POST,           true);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS,     "xmlstr=" . $xmlstr); // Problem

      

The problem occurs on the last line above when adding XML to CURLOPT_POSTFIELDS. The first encoded ampersand is treated as a delimiter for the parameter, as in the query string, and the variable / field "xmlstr" is truncated.

The solution I used was to replace the last line above ...

curl_setopt($curlHandle, CURLOPT_POSTFIELDS,     "xmlstr=" . urlencode($xmlstr));

      

Hope this helps someone.

+2


a source


The answers here have the right idea: there is some bad, possibly unprintable character in the document that breaks the parser. None of the answers above solved my problem, instead I used tr

to write a "clean" version of the file and then I was able to parse that.

<?php
try {
    $simpleXMLobject = simplexml_load_file($feed);
} catch (\Exception $ex) {
    //try to clean the file and reload it
    $tempFile = sys_get_temp_dir() . "/" . uniqid("rdc");
    shell_exec(
        "tr -cd '\11\12\15\40-\176' < " .
        escapeshellarg($feed) . " > " .
        escapeshellarg($tempFile)
    );
    try {
        $simpleXMLobject = simplexml_load_file($tempFile);
    } catch (\Exception $ex) {
        $err = $ex->getTraceAsString();
        echo die($err);
    }
}

      

0


a source


I don't see any error (either the actual XML used is different from the one provided, or the xml processor used (BTW, what is this?) Is an error).

I would recommend avoiding using CDATA sections. Use the following XML document which is the same as the (equivalent text) provided and is more readable:

<quizzes>
   <quizz>
      <title>Title</title>
      <descr>Some text here!</descr>
      <tags>one tag, second tag</tags>
      <image>http://www.site.org/displayimage.php?album=search&amp;cat=0&amp;pos=1</image>
      <results>
         <result>
            <title>Something</title>
            <descr>Some text here</descr>
            <image>http://www.site.org/displayimage.php?album=search&amp;cat=0&amp;pos=17</image>
            <id>1</id>
         </result>
      </results>
   </quizz>
</quizzes>

      

-1


a source







All Articles