4

当我在下面的 XML 中使用 DOMDocument::loadXML() 时,出现错误:

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 

在我看来,我的 CData 部分已关闭,但我仍然收到此错误。XML 看起来像这样:

<?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>

你能帮我找出问题所在吗?

4

5 回答 5

11

我发现隐藏的 XML 字符通常存在问题,所以我更喜欢像心爱的那样转义无效字符:

<?php
//$feedXml is the fetched XML content
$invalid_characters = '/[^\x9\xa\x20-\xD7FF\xE000-\xFFFD]/';
$feedXml = preg_replace($invalid_characters, '', $feedXml );
于 2016-04-28T11:10:47.147 回答
2

抱歉,如果这是题外话,因为它仅与使用 cURL 时 PHP 的特定情况有关,但是,正如 tomaszs 所述,我也发现在 PHP 中通过 cURL 传递 XML 时,& 符号可能会导致问题。我收到了一个已知的有效 XML 字符串,其中 & 符号正确编码,然后使用 cURL 将其转发到另一个地址。像这样的东西...

$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

将 XML 添加到 CURLOPT_POSTFIELDS 时,上述最后一行会出现问题。第一个编码的 & 符号被视为参数的分隔符,就像在 querstring 中一样,并且“xmlstr”变量/字段被截断。

我使用的解决方案是将上面的最后一行替换为...

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

希望这可以帮助某人。

于 2011-03-16T18:03:00.127 回答
0

这里的答案有正确的想法:文档中有某种不好的,可能是非打印的字符,这会破坏解析器。上面的答案都没有解决我的问题,而是我曾经tr编写文件的“干净”版本,然后我能够解析它,即

<?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);
    }
}
于 2017-04-25T00:04:45.917 回答
-2

我没有看到任何错误(实际使用的 XML 与提供的不同,或者使用的 xml 处理器(顺便说一句,它是什么?)有问题)。

我建议避免使用 CDATA 部分。使用以下 XML 文档,它与所提供的(文本等效)相同,并且更具可读性:

<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>
于 2010-05-09T16:12:39.213 回答
-2

我发现问题在于使用 cURL 在 PHP 中传递这个 XML。我已将其作为普通文本发送,并且此 XML 中的 & 字符被解释为下一个参数的分隔符。因此,当我逃脱此字符时,它开始正常工作。

于 2010-05-24T08:01:44.870 回答