• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

有关 SimpleXMLElement 的除错 for SegmentFault

php 搞代码 3年前 (2022-01-25) 15次浏览 已收录 0个评论
文章目录[隐藏]

我用了一个方式去抓一列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<i>*本5文来源gaodai$ma#com搞$$代**码)网@</i><img>搞代码gaodaima</img> 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


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:有关 SimpleXMLElement 的除错 for SegmentFault

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址