【问题标题】:'xmlParseEntityRef: no name' warnings while loading xml into a php file将 xml 加载到 php 文件时出现“xmlParseEntityRef:无名称”警告
【发布时间】:2011-11-28 02:23:51
【问题描述】:

我正在使用simplexml_load_file 在 php 中读取 xml。但是,在尝试加载 xml 时,它会显示警告列表

Warning: simplexml_load_file() [function.simplexml-load-file]: <project orderno="6" campaign_name="International Relief & Development" project in /home/bluecard1/public_html/test.php on line 3    
Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /home/bluecard1/public_html/test.php on line 3    
Warning: simplexml_load_file() [function.simplexml-load-file]: http://..../index.php/site/projects/:15: parser error : xmlParseEntityRef: no name in /home/bluecard1/public_html/test.php on line 3

Warning: simplexml_load_file() [function.simplexml-load-file]: ional Relief & Development" project_id="313" client_name="International Relief & in /home/bluecard1/public_html/test.php on line 3    
Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /home/bluecard1/public_html/test.php on line 3    
Warning: simplexml_load_file() [function.simplexml-load-file]: http://..../index.php/site/projects/:15: parser error : xmlParseEntityRef: no name in /home/bluecard1/public_html/test.php on line 3

如何纠正以删除这些警告?

(XML 是从 url http://..../index.php/site/projects 生成并加载到 test.php 中的变量中。我没有写权限到 index.php)

【问题讨论】:

  • XML 无效。您可能根本无法加载它。可以通过在simplexml_load_file 前面添加@ 或添加标志来抑制错误,有关详细信息,请参阅simplexml_load_file 的手册页,请删除您的问题,这是重复的。
  • 我可以看到我的答案受到了很多关注,如果这实际上是解决方案:您能否将其标记为“正确答案”?谢谢。

标签: php xml simplexml


【解决方案1】:

XML 很可能是无效的。

问题可能是“&”

$text=preg_replace('/&(?!#?[a-z0-9]+;)/g', '&amp;', $text);

将去掉“&”并将其替换为它的 HTML 代码版本...试试看。

【讨论】:

  • 谢谢。你拯救了我的一天!
  • 使用 XML 时的最佳实践是确保没有冲突的字符,您应该在解析之前替换它们
  • 谢谢,这个问题的重点是因为xml无效
  • 只是一点点补充,如果你想替换所有的 & 符号,在你的正则表达式后面加上 'g'。更新后的解决方案如下所示:$text=preg_replace('/&amp;(?!#?[a-z0-9]+;)/g', '&amp;amp;', $text);
  • 我已更新答案,使其包含全局搜索
【解决方案2】:

找到这个here ...

问题: XML 解析器返回错误“xmlParseEntityRef: noname”

原因: XML 文本中某处有一个杂散的“&”(和号字符),例如。一些文字和一些文字

解决方案:

  • 解决方案 1:删除 & 符号。
  • 解决方案 2:对 & 符号进行编码(即将 &amp;amp; 字符替换为 &amp;amp; )。读取 XML 时记得解码 文本。
  • 解决方案 3:使用 CDATA 部分(解析器将忽略 CDATA 部分中的文本。)例如。

注意:'&' '' 处理不当都会出问题。

【讨论】:

  • 我们知道这是为什么吗?此外,CDATA 部分是否仍会被呈现这些数据的浏览器拾取?我的 XML 标记中有一些 HTML 标记,我需要将它们呈现给最终用户以用作编辑工具。
【解决方案3】:

尝试先使用此函数清理 HTML:

$html = htmlspecialchars($html);

特殊字符在 HTML 中通常以不同的方式表示,这可能会让编译器感到困惑。比如&amp;amp; 变成了&amp;amp;

【讨论】:

  • 有人可以解释为什么这被否决了吗? htmlspecialchars() 是在元素数据中转换&amp;, ", &lt;, &gt; 字符的精确函数。
  • 这个答案被否决了,因为在这种情况下它不能很好地工作。使用该函数将通过将“htmlspecialchars() 而不会破坏 XML。我尝试了一些标志,但我的 XML 仍然损坏。
  • 您应该在 xml 标签的内容上使用htmlspecialchars,而不是在整个 XML 上
  • 这个答案对我帮助很大。是的,正如@gbalduzzi 所说:仅将其用于内容。
【解决方案4】:

问题

  • PHP 函数 simplexml_load_file 在尝试从 URL 加载 XML 文件时抛出解析错误 parser error : xmlParseEntityRef

原因

  • URL 返回的 XML 不是有效的 XML。它包含 &amp;amp; 值 而不是 &amp;amp;。很有可能还有其他目前不明显的错误。

我们无法控制的事情

  • 理想情况下,我们应该确保将有效的 XML 输入到 PHP simplexml_load_file 函数中,但看起来我们无法控制 XML 的创建方式。
  • 也不能强制simplexml_load_file 处理 无效的 XML 文件。它并没有给我们留下很多选择,除了 修复 XML 文件本身。

可能的解决方案

将无效的 XML 转换为有效的 XML。可以使用 PHP tidy extension 来完成。进一步的说明可以从http://php.net/manual/en/book.tidy.php找到。

一旦您确定扩展程序存在或已安装,请执行以下操作。

/**
 * As per the question asked, the URL is loaded into a variable first, 
 * which we can assume to be $xml
 */
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<project orderno="6" campaign_name="International Relief & Development for under developed nations">
    <invalid-data>Some other data containing & in it</invalid-data>
    <unclosed-tag>
</project>
XML;

/**
 * Whenever we use tidy it is best to pass some configuration options 
 * similar to $tidyConfig. In this particular case we are making sure that
 * tidy understands that our input and output is XML.
 */
$tidyConfig = array (
    'indent' => true,
    'input-xml' => true, 
    'output-xml' => true,
    'wrap' => 200
);

/**
 * Now we can use tidy to parse the string and then repair it.
 */
$tidy = new tidy;
$tidy->parseString($xml, $tidyConfig, 'utf8');
$tidy->cleanRepair();

/**
 * If we try to output the repaired XML string by echoing $tidy it should look like. 

 <?xml version="1.0" encoding="utf-8"?>
 <project orderno="6" campaign_name="International Relief &amp; Development for under developed nations">
      <invalid-data>Some other data containing &amp; in it</invalid-data>
      <unclosed-tag></unclosed-tag>
 </project> 

 * As you can see that & is now fixed in campaign_name attribute 
 * and also with-in invalid-data element. You can also see that the   
 * <unclosed-tag> which didn't had a close tag, has been fixed too.
 */
echo $tidy;

/**
 * Now when we try to use simplexml_load_string to load the clean XML. When we
 * try to print_r it should look something like below.

 SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [orderno] => 6
            [campaign_name] => International Relief & Development for under developed nations
        )

    [invalid-data] => Some other data containing & in it
    [unclosed-tag] => SimpleXMLElement Object
        (
        )

)

 */
 $simpleXmlElement = simplexml_load_string($tidy);
 print_r($simpleXmlElement);

注意

开发者应尝试将无效的 XML 与有效的 XML(由 tidy 生成)进行比较,看看使用 tidy 后是否有不良副作用。 Tidy 在正确地做到这一点方面做得非常好,但是从视觉上看到它并 100% 确定它永远不会有坏处。在我们的例子中,它应该像比较 $xml 和 $tidy 一样简单。

【讨论】:

    【解决方案5】:

    XML 无效。

    <![CDATA[ 
    {INVALID XML}
    ]]> 
    

    根据W3C,CDATA 应该包裹在所有特殊的 XML 字符上

    【讨论】:

      【解决方案6】:

      我使用组合版本:

      strip_tags(preg_replace("/&(?!#?[a-z0-9]+;)/", "&amp;",$textorhtml))
      

      【讨论】:

      • 这个工作完美。你只是缺少右括号
      【解决方案7】:

      这确实是由于字符弄乱了数据。使用 htmlentities($yourText) 对我有用(我在 xml 文档中有 html 代码)。见http://uk3.php.net/htmlentities

      【讨论】:

        【解决方案8】:

        这解决了我的问题:

        $description = strip_tags($value['Description']);
        $description=preg_replace('/&(?!#?[a-z0-9]+;)/', '&amp;', $description);
        $description= preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $description);
        $description=str_replace(' & ', ' &amp; ', html_entity_decode((htmlspecialchars_decode($description))));
        

        【讨论】:

          【解决方案9】:

          如果您在使用 opencart 时遇到此问题,请尝试编辑

          目录/控制器/扩展/feed/google_sitemap.php 有关更多信息和操作方法,请参阅:xmlparseentityref-no-name-error

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-07-07
            • 2014-01-16
            • 1970-01-01
            • 1970-01-01
            • 2014-06-22
            • 1970-01-01
            相关资源
            最近更新 更多