将 xml 加载到 php 文件时出现“xmlParseEntityRef: no name”警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7604436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:01:12  来源:igfitidea点击:

'xmlParseEntityRef: no name' warnings while loading xml into a php file

phpxmlsimplexml

提问by Rajat Gupta

I am reading an xml in php using simplexml_load_file. However while trying to load the xml it displays a list of warnings

我正在使用 .php 读取 php 中的 xml simplexml_load_file。但是,在尝试加载 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

How do I rectify to remove these warnings?

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

(XML is generated from url http://..../index.php/site/projects& loaded into a variable in the test.php. I dont have write priveleges to index.php)

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

回答by ricricucit

The XML is most probably invalid.

XML 很可能无效。

The problem could be the "&"

问题可能是“&”

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

will get rid of the "&" and replace it with it's HTML code version...give it a try.

将摆脱“&”并将其替换为它的 HTML 代码版本...试一试。

回答by King'ori Maina

Found this here ...

在这里找到了这个......

Problem:An XML parser returns the error “xmlParseEntityRef: noname”

Cause:There is a stray ‘&' (ampersand character) somewhere in the XML text eg. some text & some more text

Solution:

  • Solution 1: Remove the ampersand.
  • Solution 2: Encode the ampersand (that is replace the &character with &amp;). Remember to Decode when reading the XML text.
  • Solution 3: Use CDATA sections (text inside a CDATA section will be ignored by the parser.) eg. <![CDATA[some text & some more text]]>

Note: ‘&' ‘<' '>‘ will all give problems if not handled correctly.

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

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

解决方案:

  • 解决方案 1:删除&符号。
  • 解决方案2:编码&符号(即用 替换&字符&amp;)。阅读 XML 文本时记得解码。
  • 解决方案 3:使用 CDATA 部分(解析器将忽略 CDATA 部分内的文本。)例如。<![CDATA[一些文字和更多文字]]>

注意:如果处理不当,'&' '<' '>' 都会出现问题。

回答by Ufuk ?zdemir

Try to clean the HTML first using this function:

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

$html = htmlspecialchars($html);

Special chars are usually represented differently in HTML and it might be confusing for the compiler. Like &becomes &amp;.

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

回答by Reign.85

I use a combined version :

我使用组合版本:

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

回答by Kamal Soni

PROBLEM

问题

  • PHP function simplexml_load_fileis throwing parsing error parser error : xmlParseEntityRefwhile trying to load the XML file from a URL.
  • PHP 函数在尝试从 URL 加载 XML 文件时simplexml_load_file抛出解析错误parser error : xmlParseEntityRef

CAUSE

原因

  • XML returned by the URL is not a valid XML. It contains &value instead of &amp;. It is quite possible that there are other errors which aren't obvious at this point of time.
  • URL 返回的 XML 不是有效的 XML。它包含&value 而不是&amp;. 很可能还有其他目前不明显的错误。

THINGS OUT OF OUR CONTROL

我们无法控制的事情

  • Ideally, we should make sure that a valid XML is feed into PHP simplexml_load_filefunction, but it looks like we don't have any control over how the XML is created.
  • It is also not possible to force simplexml_load_fileto process an invalid XML file. It does not leave us with many options, other than fixing the XML file itself.
  • 理想情况下,我们应该确保将有效的 XML 输入到 PHPsimplexml_load_file函数中,但看起来我们无法控制 XML 的创建方式。
  • 也无法强制simplexml_load_file处理无效的 XML 文件。除了修复 XML 文件本身之外,它并没有给我们留下很多选择。

POSSIBLE SOLUTION

可能的解决方案

Convert Invalid XML to Valid XML. It can be done using PHP tidy extension. Further instructions can be found from http://php.net/manual/en/book.tidy.php

将无效的 XML 转换为有效的 XML。可以使用PHP tidy extension. 可以从http://php.net/manual/en/book.tidy.php找到更多说明

Once you are sure that the extension exists or is installed, please do the following.

确定扩展存在或已安装后,请执行以下操作。

/**
 * 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);

CAUTION

警告

The developer should try to compare the invalid XML with a valid XML (generated by tidy), to see there are no adverse side effects after using tidy. Tidy does an extremely good job of doing it correctly, but it never hurts to see it visually and to be 100% sure. In our case it should be as simple as comparing $xml with $tidy.

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

回答by Edwin Daniels

The XML is invalid.

XML 无效。

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

CDATA should be wrapped around all special XML characters, as per W3C

根据W3C,CDATA 应该包含在所有特殊的 XML 字符周围

回答by Guillaume

This is in deed due to characters messing around with the data. Using htmlentities($yourText)worked for me (I had html code inside the xml document). See http://uk3.php.net/htmlentities.

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

回答by Malki Mohamed

This solve my problème:

这解决了我的问题:

$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))));

回答by Akila Wickramasekara

If you are getting this issue with opencart try editing

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

catalog/controller/extension/feed/google_sitemap.php For More info and How to do it refer this: xmlparseentityref-no-name-error

catalog/controller/extension/feed/google_sitemap.php 有关更多信息以及如何操作,请参阅:xmlparseentityref-no-name-error