2014-07-02 63 views
1

我使用simplexml_load_file,期望它可以在url上使用,奇怪的是一些數據丟失。simplexml_load_file只返回xml的部分數據

我的PHP

$url = "http://www.thestar.com.my/RSS/Lifestyle/Women/"; 
$xml = simplexml_load_file($url); 
echo "<pre>"; 
print_r($xml); 

部分結果

[item] => Array 
     (
      [0] => SimpleXMLElement Object 
       (
        [guid] => {936842CF-550D-4CAB-A840-23BB4E766B8E} 
        [link] => http://www.thestar.com.my/Lifestyle/Women/Fashion/2014/07/02/Jamy-Yang-Mingjie-Shared-travelling-values 
        [title] => SimpleXMLElement Object 
         (
         ) 

        [description] => SimpleXMLElement Object 
         (
         ) 

        [pubDate] => Wed, 02 Jul 2014 00:00:00 +08:00 
        [enclosure] => SimpleXMLElement Object 
         (
          [@attributes] => Array 
           (
            [url] => http://www.thestar.com.my/~/media/Images/TSOL/Photos-Gallery/features/2014/07/02/JamyYangMingjie020714.ashx?crop=1&w=460&h=345& 
            [length] => 
            [type] => image/jpeg 
           ) 

         ) 

       ) 

item->title成爲SimpleXMLElement對象?

+0

是的,這**完全沒有問題**。它總是一個SimpleXMLElement,它只是在這裏以一種有用的方式不能對SimpleXMLElement使用'print_r'。查看鏈接副本中的所有答案。 – hakre

回答

-1

您忘記添加其他參數來獲取CDATA字符數據。試試這個:

$url = "http://www.thestar.com.my/RSS/Lifestyle/Women/"; 
$xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA); 
echo "<pre>"; 
print_r($xml); 
+0

-1:誤導。 – hakre

1

當我查看該網址的源代碼時,所有字符串都用CDATA標籤包裝,包括標題。據當時php:

「在代碼-B點NL沃特」,「正確地提取從CDATA值只是確保你使用強制轉換運算符鑄的SimpleXML元素爲一個字符串值」