【问题标题】:CData section not finished problemCData部分未完成问题
【发布时间】:2011-02-17 09:25:59
【问题描述】:

当我在下面的 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>

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

【问题讨论】:

  • “XML 看起来像这样” - “看起来像”还是“完全一样”?如果您不希望我们看到实际的 url,您可能不仅要更改文档,还要更改错误消息。
  • @VolkerK 谢谢,我不想在这里给任何网站做广告。

标签: php xml dom parsing domdocument


【解决方案1】:

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

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

【讨论】:

  • 这解决了我的问题。
  • 这也解决了我的问题。你能解释一下我们在这里替换的是什么吗?
【解决方案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));

希望这对某人有所帮助。

【讨论】:

    【解决方案3】:

    这里的答案有正确的想法:文档中存在某种错误的、可能无法打印的字符,这会破坏解析器。上面的答案都没有解决我的问题,而是我使用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);
        }
    }
    

    【讨论】:

      【解决方案4】:

      我没有看到任何错误(实际使用的 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>
      

      【讨论】:

      • 如果您要解析从第 3 方源提供给您的 XML,这不是一个真正的选择
      【解决方案5】:

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

      【讨论】:

      • 你如何逃避它,请在你的答案中添加代码/细节。
      • 请扩展您的答案,包括代码示例。
      猜你喜欢
      • 2017-10-30
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多