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]+;)/', '&', $text);
Run Code Online (Sandbox Code Playgroud)
将摆脱"&"并用它的HTML代码版本替换它...尝试一下.
小智 11
首先尝试使用此函数清理HTML:
$html = htmlspecialchars($html);
Run Code Online (Sandbox Code Playgroud)
特殊字符通常在HTML中表示不同,它可能会使编译器感到困惑.喜欢&
成为&
.
我使用组合版本:
strip_tags(preg_replace("/&(?!#?[a-z0-9]+;)/", "&",$textorhtml))
Run Code Online (Sandbox Code Playgroud)
问题
simplexml_load_file
会抛出解析错误parser
error : xmlParseEntityRef
.原因
&
价值而不是&
.很可能还有其他错误在这个时间点并不明显.出于我们的控制
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 & 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);
Run Code Online (Sandbox Code Playgroud)
警告
开发人员应该尝试将无效的XML与有效的XML(由tidy生成)进行比较,以确保在使用整洁后没有不良副作用.Tidy做得非常好,但是从视觉上看它并且100%确定它永远不会伤害.在我们的例子中,它应该像将$ xml与$ tidy进行比较一样简单.
XML无效.
<![CDATA[
{INVALID XML}
]]>
Run Code Online (Sandbox Code Playgroud)
根据W3C,CDATA应该包含所有特殊的XML字符
归档时间: |
|
查看次数: |
99660 次 |
最近记录: |