有关 SimpleXMLElement 的除错 for SegmentFault
程序员文章站
2022-05-22 08:49:53
...
我用了一个方式去抓一列something…之后我要解析成xml的格式
当然是可行的,但时不及会出错 于是我写下了
当然是可行的,但时不及会出错 于是我写下了
try
{
$sports = new SimpleXMLElement($parsedJson);
}
catch (Exception $e)
{
Print_r($e)
}
结果为
Warning: SimpleXMLElement::__construct(): Entity: line 1: parser error : Space required after the Public Identifier in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): ^ in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): Entity: line 6: parser error : Opening and ending tag mismatch: hr line 5 and BODY in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): ^ in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): Entity: line 6: parser error : Opening and ending tag mismatch: BODY line 4 and HTML in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): ^ in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): Entity: line 7: parser error : Premature end of data in tag HEAD line 2 in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): ^ in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): Entity: line 7: parser error : Premature end of data in tag HTML line 2 in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): ^ in /home/ns/www/admin/gamble/baseball.php on line 121
我想到我之前用 curl 抓东西时,用过 json_decode
当时我用这样的方式来让程序自动重抓
do{
$data = @json_decode(file_get_contents($url), true);
}while($data == false);
我想请教的是
如果我的 new SimpleXMLElement 失败了,我也可以用类似的方式作吗?
Do
{
抓资料;
$data=@json_decode(something);
$xml = new SimpleXMLElement($data);
}while($xml == false);
如果 new SimpleXMLElemen 发生以上的错误,$xml 会 等于 false吗?
求解
回复内容:
我用了一个方式去抓一列something…之后我要解析成xml的格式
当然是可行的,但时不及会出错 于是我写下了
try
{
$sports = new SimpleXMLElement($parsedJson);
}
catch (Exception $e)
{
Print_r($e)
}
结果为
Warning: SimpleXMLElement::__construct(): Entity: line 1: parser error : Space required after the Public Identifier in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): ^ in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): Entity: line 6: parser error : Opening and ending tag mismatch: hr line 5 and BODY in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): ^ in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): Entity: line 6: parser error : Opening and ending tag mismatch: BODY line 4 and HTML in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): ^ in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): Entity: line 7: parser error : Premature end of data in tag HEAD line 2 in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): ^ in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): Entity: line 7: parser error : Premature end of data in tag HTML line 2 in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): ^ in /home/ns/www/admin/gamble/baseball.php on line 121
我想到我之前用 curl 抓东西时,用过 json_decode
当时我用这样的方式来让程序自动重抓
do{
$data = @json_decode(file_get_contents($url), true);
}while($data == false);
我想请教的是
如果我的 new SimpleXMLElement 失败了,我也可以用类似的方式作吗?
Do
{
抓资料;
$data=@json_decode(something);
$xml = new SimpleXMLElement($data);
}while($xml == false);
如果 new SimpleXMLElemen 发生以上的错误,$xml 会 等于 false吗?
求解
首先,问题可能是因为xml捕获了一个非正常XML格式的数据,如果要捕获错误,可以这样
/**
* 解析XML格式的字符串
*
* @param string $str
* @return 解析正确就返回解析结果,否则返回false,说明字符串不是XML格式
*/
function xml_parser($str){
$xml_parser = xml_parser_create();
if(!xml_parse($xml_parser,$str,true)){
xml_parser_free($xml_parser);
return false;
} else {
return (json_decode(json_encode(simplexml_load_string($str)),true));
}
}
另外,你通过file_get_contents($url),直接抓取URL,是有可能抓不全数据的哦,这里我推荐一个HTTP请求的类:
简单而强大的HTTP请求类,来自类库:lv
http://levi.cg.am/archives/3534