有关 SimpleXMLElement 的除错 for php
大家讲道理
大家讲道理 2017-04-10 15:06:28
0
1
474

我用了一个方式去抓一列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吗?
求解

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

모든 응답(1)
阿神

首先,问题可能是因为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

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!