'xmlParseEntityRef: 无名称'将 xml 加载到 php 文件时出现警告

发布于 2024-12-07 17:31:53 字数 1290 浏览 4 评论 0原文

我正在使用 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 中的变量中。我没有写入权限索引.php)

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

箜明 2024-12-14 17:31:53

XML 很可能是无效的。
问题可能是“&”

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

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

The XML is most probably invalid.
The problem could be the "&"

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

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

嘿哥们儿 2024-12-14 17:31:53

找到这个这里...

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

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

解决方案:

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

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

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 & ). 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.

红衣飘飘貌似仙 2024-12-14 17:31:53

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

$html = htmlspecialchars($html);

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

Try to clean the HTML first using this function:

$html = htmlspecialchars($html);

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

难以启齿的温柔 2024-12-14 17:31:53

我使用组合版本:

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

I use a combined version :

strip_tags(preg_replace("/&(?!#?[a-z0-9]+;)/", "&",$textorhtml))
橙味迷妹 2024-12-14 17:31:53

问题

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

原因

  • URL 返回的 XML 不是有效的 XML。它包含 &
    而不是 &。很可能还存在其他此时并不明显的错误。

我们无法控制的事情

  • 理想情况下,我们应该确保将有效的 XML 馈送到 PHP simplexml_load_file 函数中,但看起来我们无法控制如何XML 已创建。
  • 也不可能强制 simplexml_load_file 处理
    XML 文件无效。除了
    修复 XML 文件本身。

可能的解决方案

将无效 XML 转换为有效 XML。可以使用 PHP tidy 扩展 来完成。可以从 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 & Development for under developed nations">
      <invalid-data>Some other data containing & 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 一样简单。

PROBLEM

  • PHP function simplexml_load_file is throwing parsing error parser
    error : xmlParseEntityRef
    while trying to load the XML file from a URL.

CAUSE

  • XML returned by the URL is not a valid XML. It contains & value
    instead of &. It is quite possible that there are other errors which aren't obvious at this point of time.

THINGS OUT OF OUR CONTROL

  • Ideally, we should make sure that a valid XML is feed into PHP simplexml_load_file function, 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_file to process an
    invalid XML file. It does not leave us with many options, other than
    fixing the XML file itself.

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

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 & Development for under developed nations">
      <invalid-data>Some other data containing & 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.

无人问我粥可暖 2024-12-14 17:31:53

XML 无效。

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

CDATA 应包含所有特殊 XML 字符,按照 W3C

The XML is invalid.

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

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

臻嫒无言 2024-12-14 17:31:53

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

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 https://www.php.net/htmlentities.

回忆凄美了谁 2024-12-14 17:31:53

这解决了我的问题:

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

This solve my problème:

$description = strip_tags($value['Description']);
$description=preg_replace('/&(?!#?[a-z0-9]+;)/', '&', $description);
$description= preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $description);
$description=str_replace(' & ', ' & ', html_entity_decode((htmlspecialchars_decode($description))));
痕至 2024-12-14 17:31:53

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

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


If you are getting this issue with opencart try editing

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文