将xml加载到php文件时,'xmlParseEntityRef:没有名称'警告

Raj*_*pta 82 php xml simplexml

我正在使用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
Run Code Online (Sandbox Code Playgroud)

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

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

ric*_*cit 134

XML很可能是无效的.

问题可能是"&"

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

将摆脱"&"并用它的HTML代码版本替换它...尝试一下.

  • 谢谢。你救了我的一天! (2认同)
  • 使用XML的最佳实践是确保没有冲突的字符,并且您应该在解析之前替换它们 (2认同)
  • 谢谢,这个问题的重点是因为xml无效 (2认同)
  • 全局搜索隐含在 preg_replace 中。如果您在末尾添加“g”,则会收到警告:警告:preg_replace():未知修饰符“g” (2认同)

Kin*_*ina 66

在这里发现了......

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

原因:在XML文本中的某处有一个迷路'&'(&符号).一些文字和一些文字

解:

  • 解决方案1:取下&符号.
  • 解决方案2:对&符号进行编码(将'&'字符替换为'&').记得在阅读XML文本时解码.
  • 解决方案3:使用CDATA部分(解析器将忽略CDATA部分内的文本.)例如.<![CDATA [some text&some more text]]>

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

  • 今天这救了我. (8认同)

小智 11

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

$html = htmlspecialchars($html);
Run Code Online (Sandbox Code Playgroud)

特殊字符通常在HTML中表示不同,它可能会使编译器感到困惑.喜欢&成为&amp;.

  • 这个答案被低估了,因为在这种情况下它不能很好地工作.使用该函数将通过将"<"转换为"&lt;"来完全破坏XML.我没有意识到你可以使用`htmlspecialchars()`而不是破坏XML.我试了几个标志,我的XML仍然破了. (6认同)
  • 您应该在 xml 标记的内容上使用“htmlspecialchars”,而不是在整个 XML 上 (2认同)

Rei*_*.85 7

我使用组合版本:

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


Kam*_*oni 7

问题

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

原因

  • URL返回的XML不是有效的XML.它包含&价值而不是&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);
Run Code Online (Sandbox Code Playgroud)

警告

开发人员应该尝试将无效的XML与有效的XML(由tidy生成)进行比较,以确保在使用整洁后没有不良副作用.Tidy做得非常好,但是从视觉上看它并且100%确定它永远不会伤害.在我们的例子中,它应该像将$ xml与$ tidy进行比较一样简单.


Edw*_*els 6

XML无效.

<![CDATA[ 
{INVALID XML}
]]> 
Run Code Online (Sandbox Code Playgroud)

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