2012-06-08 67 views
0

這是我的代碼。用PHP和simplexml_load_file解析XML CDATA

$xml = simplexml_load_file("http://www.metacafe.com/api/item/8373557/", NULL, LIBXML_NOCDATA); 
$thumbnail = $xml->xpath("/rss/channel/item/media:thumbnail/@url"); 
$duration = $xml->xpath("/rss/channel/item/media:content/@duration"); 
$title = $xml->xpath ("/rss/channel/item/title"); 
$desc = $xml->xpath ("/rss/channel/item/description/"); 

下面是XML文件內容:

<?xml version="1.0" encoding="utf-8"?> 
     <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" source="Metacafe"> 
      <title>Metacafe</title> 
      <channel> 
        <title></title> 
        <link>http://www.metacafe.com/watch/8373557/</link> 
        <image> 
         <url>http://s.mcstatic.com/Images/MCLogo4RSS.jpg</url> 
         <link>http://www.metacafe.com</link> 
         <title>Metacafe</title> 
         <height>65</height> 
         <width>229</width> 
        </image> 
        <description></description> 
        <item> 
      <id>8373557</id> 
      <author>Revision3</author> 
      <title>Make Your Photos Look Pro with Pix: Pixel Mixer for IOS and Android - Snapp</title> 
      <link>http://www.metacafe.com/watch/8373557/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp/</link> 
        <rank>3.70</rank> 
      <category>Science &amp; Tech</category> 
      <description> 
      <![CDATA[ 
        <a href="http://www.metacafe.com/watch/8373557/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp/"><img src="http://s6.mcstatic.com/thumb/8373557/22302729/4/directors_cut/0/1/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp.jpg?v=3" align="right" border="0" alt="Make Your Photos Look Pro with Pix: Pixel Mixer for IOS and Android - Snapp" vspace="4" hspace="4" width="134" height="78" /></a> 
        <p> 
        Create your own professional quality images with this free app from developer colonO. Pix: Pixel Mixer is simple, beautiful, free and available on both iOS and Android.     <br>Ranked <strong>3.70</strong>/5 | 4971 views | <a href="http://www.metacafe.com/watch/8373557/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp/">0 comments</a><br/> 
        </p> 
        <p> 
         <a href="http://www.metacafe.com/watch/8373557/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp/"><strong>Click here to watch the video</strong></a> (01:02)<br/> 
         Submitted By:      <a href="http://www.metacafe.com/channels/Revision3/">Revision3</a><br/> 
         Tags: 
         <a href="http://www.metacafe.com/topics/pictures/">Pictures</a>&nbsp;<a href="http://www.metacafe.com/topics/pixel_mixer/">Pixel Mixer</a>&nbsp;<a href="http://www.metacafe.com/topics/photo_filters/">Photo Filters</a>&nbsp;<a href="http://www.metacafe.com/topics/filters_for/">Filters For</a>&nbsp;<a href="http://www.metacafe.com/topics/ios_and/">Ios And</a>&nbsp;<a href="http://www.metacafe.com/topics/android/">Android</a>&nbsp;<a href="http://www.metacafe.com/topics/ios_review/">Ios Review</a>&nbsp;<a href="http://www.metacafe.com/topics/instagram_filters/">Instagram Filters</a>&nbsp;      <br/> 
         Categories: <a href='http://www.metacafe.com/videos/science_and_tech/'>Science &amp; Tech</a>     </p> 
       ]]> 
      </description> 
         <guid isPermaLink="true">http://www.metacafe.com/watch/8373557/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp/</guid> 
      <pubDate>16-Apr-12 +0000</pubDate> 
         <media:player url="http://www.metacafe.com/watch/8373557/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp/" /> 
         <media:content url="http://www.metacafe.com/fplayer/8373557/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp.swf" type="application/x-shockwave-flash" medium="video" height="360" width="640" duration="62" />    <media:thumbnail url="http://s6.mcstatic.com/thumb/8373557/22302729/4/catalog_item5/0/1/make_your_photos_look_pro_with_pix_pixel_mixer_for_ios_and_android_snapp.jpg?v=3" /> 
      <media:title>Make Your Photos Look Pro with Pix: Pixel Mixer for IOS and Android - Snapp</media:title> 
      <media:keywords>Pictures,Pixel Mixer,Photo Filters,Filters For,Ios And,Android,Ios Review,Instagram Filters</media:keywords> 
         <media:description>Create your own professional quality images with this free app from developer colonO. Pix: Pixel Mixer is simple, beautiful, free and available on both iOS and Android.</media:description> 
         <media:credit>Revision3</media:credit> 
          <media:rating scheme="urn:simple">nonadult</media:rating> 
       </item> 
      </channel></rss> 

我想提取標籤內容,不知道表達我應該做的打印?我成功獲取縮略圖,持續時間,標題,但未能在CDATA /內部描述中獲取標籤。

任何幫助?

回答

2

在第一步中,您需要使用$xml->rss->channel->item->description或使用xpath表達式來獲取<description>標記的內容。然後你使用這個內容的正則表達式來獲取帶有標籤名稱的鏈接。正則表達式應該是這樣的:

~<a href="http://www.metacafe.com/topics/(?P<foobar>[^"]+)/">(?P<tag>[^<]+)</a>~ 

另一種方法是,要求您可以使用從一個項目獲取標籤的API。