有关 SimpleXMLElement 的除错 for SegmentFault
Jun 06, 2016 pm 08:41 PM
我用了一个方式去抓一列something…之后我要解析成xml的格式
当然是可行的,但时不及会出错 于是我写下了
<code>try { $sports = new SimpleXMLElement($parsedJson); } catch (Exception $e) { Print_r($e) } </code>
结果为
<code>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 </code>
我想到我之前用 curl 抓东西时,用过 json_decode
当时我用这样的方式来让程序自动重抓
<code>do{ $data = @json_decode(file_get_contents($url), true); }while($data == false); </code>
我想请教的是
如果我的 new SimpleXMLElement 失败了,我也可以用类似的方式作吗?
<code>Do { 抓资料; $data=@json_decode(something); $xml = new SimpleXMLElement($data); }while($xml == false); </code>
如果 new SimpleXMLElemen 发生以上的错误,$xml 会 等于 false吗?
求解
回复内容:
我用了一个方式去抓一列something…之后我要解析成xml的格式
当然是可行的,但时不及会出错 于是我写下了
<code>try { $sports = new SimpleXMLElement($parsedJson); } catch (Exception $e) { Print_r($e) } </code>
结果为
<code>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 </code>
我想到我之前用 curl 抓东西时,用过 json_decode
当时我用这样的方式来让程序自动重抓
<code>do{ $data = @json_decode(file_get_contents($url), true); }while($data == false); </code>
我想请教的是
如果我的 new SimpleXMLElement 失败了,我也可以用类似的方式作吗?
<code>Do { 抓资料; $data=@json_decode(something); $xml = new SimpleXMLElement($data); }while($xml == false); </code>
如果 new SimpleXMLElemen 发生以上的错误,$xml 会 等于 false吗?
求解
首先,问题可能是因为xml捕获了一个非正常XML格式的数据,如果要捕获错误,可以这样
<code>/** * 解析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)); } } </code>
另外,你通过file_get_contents($url),直接抓取URL,是有可能抓不全数据的哦,这里我推荐一个HTTP请求的类:
简单而强大的HTTP请求类,来自类库:lv
http://levi.cg.am/archives/3534

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian

Comment configurer Visual Studio Code (VS Code) pour le développement PHP
