Php 防弹simplexmlement

Php 防弹simplexmlement,php,html-parsing,simplexml,domdocument,Php,Html Parsing,Simplexml,Domdocument,每个人都知道,我们应该总是使用DOM技术而不是正则表达式来从HTML中提取内容,但我觉得我永远不会相信SimpleXML扩展或类似的扩展 我现在正在编写一个OpenID实现,我尝试使用SimpleXML进行HTML发现,但我的第一次测试(使用alixaxel.myopenid.com)产生了很多错误: Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 27: parser error :

每个人都知道,我们应该总是使用DOM技术而不是正则表达式来从HTML中提取内容,但我觉得我永远不会相信SimpleXML扩展或类似的扩展

我现在正在编写一个OpenID实现,我尝试使用SimpleXML进行HTML发现,但我的第一次测试(使用alixaxel.myopenid.com)产生了很多错误:

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 27: parser error : Opening and ending tag mismatch: link line 11 and head in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: </head> in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 64: parser error : Entity 'copy' not defined in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: &copy; 2008 <a href="http://janrain.com/">JanRain, Inc.</a> in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 66: parser error : Entity 'trade' not defined in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: myOpenID&trade; and the myOpenID&trade; website are in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 66: parser error : Entity 'trade' not defined in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: myOpenID&trade; and the myOpenID&trade; website are in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 77: parser error : Opening and ending tag mismatch: link line 8 and html in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: </html> in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 78: parser error : Premature end of data in tag head line 3 in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 78: parser error : Premature end of data in tag html line 2 in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: in E:\xampplite\htdocs\index.php on line 6

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in E:\xampplite\htdocs\index.php on line 6
警告:simplexml\u load\u string()[function.simplexml load string]:实体:第27行:解析器错误:开头和结尾标记不匹配:第6行的链接行11和标题为E:\xamplite\htdocs\index.php
警告:simplexml\u load\u string()[function.simplexml load string]:在第6行的E:\xamplite\htdocs\index.php中
警告:simplexml_load_string()[function.simplexml load string]:^在第6行的E:\xamplite\htdocs\index.php中
警告:simplexml\u load\u string()[function.simplexml load string]:实体:第64行:解析器错误:第6行的E:\xamplite\htdocs\index.php中未定义实体“copy”
警告:simplexml加载字符串()[函数.simplexml加载字符串]:©;2008年,在第6行的E:\xamplite\htdocs\index.php中
警告:simplexml_load_string()[function.simplexml load string]:^在第6行的E:\xamplite\htdocs\index.php中
警告:simplexml\u load\u string()[function.simplexml load string]:实体:第66行:解析器错误:第6行的E:\xamplite\htdocs\index.php中未定义实体“trade”
警告:simplexml\u load\u string()[function.simplexml load string]:myOpenID&trade;以及myOpenID&trade;网站位于第6行的E:\xamplite\htdocs\index.php中
警告:simplexml_load_string()[function.simplexml load string]:^在第6行的E:\xamplite\htdocs\index.php中
警告:simplexml\u load\u string()[function.simplexml load string]:实体:第66行:解析器错误:第6行的E:\xamplite\htdocs\index.php中未定义实体“trade”
警告:simplexml\u load\u string()[function.simplexml load string]:myOpenID&trade;以及myOpenID&trade;网站位于第6行的E:\xamplite\htdocs\index.php中
警告:simplexml_load_string()[function.simplexml load string]:^在第6行的E:\xamplite\htdocs\index.php中
警告:simplexml_load_string()[function.simplexml load string]:实体:第77行:解析器错误:开头和结尾标记不匹配:第6行E:\xamplite\htdocs\index.php中的链接行8和html
警告:simplexml\u load\u string()[function.simplexml load string]:在第6行的E:\xamplite\htdocs\index.php中
警告:simplexml_load_string()[function.simplexml load string]:^在第6行的E:\xamplite\htdocs\index.php中
警告:simplexml\u load\u string()[function.simplexml load string]:实体:第78行:解析器错误:第6行E:\xamplite\htdocs\index.php中标记头第3行的数据过早结束
警告:simplexml\u load\u string()[function.simplexml load string]:在第6行的E:\xamplite\htdocs\index.php中
警告:simplexml_load_string()[function.simplexml load string]:^在第6行的E:\xamplite\htdocs\index.php中
警告:simplexml\u load\u string()[function.simplexml load string]:实体:第78行:解析器错误:第6行E:\xamplite\htdocs\index.php中标记html第2行的数据过早结束
警告:simplexml\u load\u string()[function.simplexml load string]:在第6行的E:\xamplite\htdocs\index.php中
警告:simplexml_load_string()[function.simplexml load string]:^在第6行的E:\xamplite\htdocs\index.php中
我记得有一种方法可以让SimpleXML始终解析一个文件,不管文档是否包含错误——我记不起具体的实现,但我认为它涉及到使用DOMDocument。确保SimpleXML始终解析任何给定文档的最佳方法是什么


请不要建议使用Tidy,我认为扩展速度慢,而且在许多系统上都不可用。

您可以尝试使用SAX解析器。。。对错误更具鲁棒性


在大型XML上可能效率不高。

您可以使用加载HTML,然后将结果导入SimpleXML

IIRC,它仍然会被一些东西噎住,但它会接受现实世界中破碎网站中存在的几乎任何东西

$html = '<html><head><body><div>stuff & stuff</body></html>';

// disable PHP errors
$old = libxml_use_internal_errors(true);

$dom = new DOMDocument;
$dom->loadHTML($html);

// restore the old behaviour
libxml_use_internal_errors($old);

$sxe = simplexml_import_dom($dom);
die($sxe->asXML());
$html='stuff&stuff';
//禁用PHP错误
$old=libxml\u使用\u内部错误(true);
$dom=新的DOMDocument;
$dom->loadHTML($html);
//恢复旧习
libxml\u使用\u内部错误($old);
$sxe=simplexml\u import\u dom($dom);
die($sxe->asXML());

Uff!那更好。。。这个解决方案是防弹的吗?我能相信它能解析任何文档吗?@Alix很可能不会解析任何文档。如果HTML太坏,那么它就太坏了。但对于大多数破损的HTML来说,这应该是可行的。另一种选择是使用XmlReader。这是一个pull解析器,让您定义对哪个元素执行什么操作。对于底层libxml扩展,还有许多配置选项,您可以使用这些选项来影响解析。