有關 SimpleXMLElement 的除錯 for SegmentFault

來源:互聯網
上載者:User
我用了一個方式去抓一列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

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.