分享
 
 
 

用php5的simplexml解析各種feed

王朝php·作者佚名  2008-12-23
窄屏简体版  字體: |||超大  

用simplexml處理atom數據

很多博客使用atom來輸出數據,但是atom使用了名稱空間(namespace),所以現在請求被命名的元素和本地名稱時必須指定名稱空間統一資源標識符(URI),還有一點就是simplexml的xpath方法無法直接query這個xml tree。

從 PHP 5.1 版開始,SimpleXML 可以直接對帶名稱空間的文檔使用 XPath 查詢。和通常一樣,XPath 位置路徑必須使用名稱空間前綴,即使搜索的文檔使用默認名稱空間也仍然如此。registerXPathNamespace() 函數把前綴和後續查詢中使用的名稱空間 URL 聯系在一起。

下面是使用xpath查詢atom文檔title元素的例子:

PLAIN TEXT

CODE:

$atom = simplexml_load_file('http://www.ooso.net/index.php/feed/atom');

$atom->registerXPathNamespace('atom','http://www.w3.org/2005/Atom');

$titles = $atom->xpath('//atom:title');

foreach($titles as $title)

echo"<h2>". $title ."</h2>";

用simplexml處理rss數據

wordpress可以輸出rss2的數據源,這裏面也有一些不同的namespace,比如dc。一個使用simplexml解析rss2的例子:

PLAIN TEXT

PHP:

$ns=array(

'content'=>'http://purl.org/rss/1.0/modules/content/',

'wfw'=>'http://wellformedweb.org/CommentAPI/',

'dc'=>'http://purl.org/dc/elements/1.1/'

);

$articles=array();

// step 1: 獲得feed

$blogUrl='http://www.ooso.net/index.php/feed/rss2';

$xml= simplexml_load_url($blogUrl);

// step 2: 獲得channel metadata

$channel=array();

$channel['title'] =$xml->channel->title;

$channel['link'] =$xml->channel->link;

$channel['description']=$xml->channel->description;

$channel['pubDate'] =$xml->pubDate;

$channel['timestamp'] =strtotime($xml->pubDate);

$channel['generator'] =$xml->generator;

$channel['language'] =$xml->language;

// step 3: 獲得articles

foreach($xml->channel->itemas$item){

$article=array();

$article['channel']=$blog;

$article['title']=$item->title;

$article['link']=$item->link;

$article['comments']=$item->comments;

$article['pubDate']=$item->pubDate;

$article['timestamp']=strtotime($item->pubDate);

$article['description']=(string)trim($item->description);

$article['isPermaLink']=$item->guid['isPermaLink'];

// get data held in namespaces

$content=$item->children($ns['content']);

$dc =$item->children($ns['dc']);

$wfw =$item->children($ns['wfw']);

$article['creator']=(string)$dc->creator;

foreach($dc->subjectas$subject)

$article['subject'][]=(string)$subject;

$article['content']=(string)trim($content->encoded);

$article['commentRss']=$wfw->commentRss;

// add this article to the list

$articles[$article['timestamp']]=$article;

}

這個例子中,使用children方法來獲得名稱空間中的數據:

PLAIN TEXT

PHP:

$dc =$item->children($ns['dc']);

 
 
 
免責聲明:本文為網絡用戶發布,其觀點僅代表作者個人觀點,與本站無關,本站僅提供信息存儲服務。文中陳述內容未經本站證實,其真實性、完整性、及時性本站不作任何保證或承諾,請讀者僅作參考,並請自行核實相關內容。
用simplexml處理atom數據 很多博客使用atom來輸出數據,但是atom使用了名稱空間(namespace),所以現在請求被命名的元素和本地名稱時必須指定名稱空間統一資源標識符(URI),還有一點就是simplexml的xpath方法無法直接query這個xml tree。 從 PHP 5.1 版開始,SimpleXML 可以直接對帶名稱空間的文檔使用 XPath 查詢。和通常一樣,XPath 位置路徑必須使用名稱空間前綴,即使搜索的文檔使用默認名稱空間也仍然如此。registerXPathNamespace() 函數把前綴和後續查詢中使用的名稱空間 URL 聯系在一起。 下面是使用xpath查詢atom文檔title元素的例子: PLAIN TEXT CODE: $atom = simplexml_load_file('http://www.ooso.net/index.php/feed/atom'); $atom->registerXPathNamespace('atom','http://www.w3.org/2005/Atom'); $titles = $atom->xpath('//atom:title'); foreach($titles as $title) echo"<h2>". $title ."</h2>"; 用simplexml處理rss數據 wordpress可以輸出rss2的數據源,這裏面也有一些不同的namespace,比如dc。一個使用simplexml解析rss2的例子: PLAIN TEXT PHP: $ns=array( 'content'=>'http://purl.org/rss/1.0/modules/content/', 'wfw'=>'http://wellformedweb.org/CommentAPI/', 'dc'=>'http://purl.org/dc/elements/1.1/' ); $articles=array(); // step 1: 獲得feed $blogUrl='http://www.ooso.net/index.php/feed/rss2'; $xml= simplexml_load_url($blogUrl); // step 2: 獲得channel metadata $channel=array(); $channel['title'] =$xml->channel->title; $channel['link'] =$xml->channel->link; $channel['description']=$xml->channel->description; $channel['pubDate'] =$xml->pubDate; $channel['timestamp'] =strtotime($xml->pubDate); $channel['generator'] =$xml->generator; $channel['language'] =$xml->language; // step 3: 獲得articles foreach($xml->channel->itemas$item){ $article=array(); $article['channel']=$blog; $article['title']=$item->title; $article['link']=$item->link; $article['comments']=$item->comments; $article['pubDate']=$item->pubDate; $article['timestamp']=strtotime($item->pubDate); $article['description']=(string)trim($item->description); $article['isPermaLink']=$item->guid['isPermaLink']; // get data held in namespaces $content=$item->children($ns['content']); $dc =$item->children($ns['dc']); $wfw =$item->children($ns['wfw']); $article['creator']=(string)$dc->creator; foreach($dc->subjectas$subject) $article['subject'][]=(string)$subject; $article['content']=(string)trim($content->encoded); $article['commentRss']=$wfw->commentRss; // add this article to the list $articles[$article['timestamp']]=$article; } 這個例子中,使用children方法來獲得名稱空間中的數據: PLAIN TEXT PHP: $dc =$item->children($ns['dc']);
2023年上半年GDP全球前十五強
 百态   2023-10-24
美眾議院議長啟動對拜登的彈劾調查
 百态   2023-09-13
上海、濟南、武漢等多地出現不明墜落物
 探索   2023-09-06
印度或要將國名改為「巴拉特」
 百态   2023-09-06
男子為女友送行,買票不登機被捕
 百态   2023-08-20
手機地震預警功能怎麽開?
 干货   2023-08-06
女子4年賣2套房花700多萬做美容:不但沒變美臉,面部還出現變形
 百态   2023-08-04
住戶一樓被水淹 還衝來8頭豬
 百态   2023-07-31
女子體內爬出大量瓜子狀活蟲
 百态   2023-07-25
地球連續35年收到神秘規律性信號,網友:不要回答!
 探索   2023-07-21
全球鎵價格本周大漲27%
 探索   2023-07-09
錢都流向了那些不缺錢的人,苦都留給了能吃苦的人
 探索   2023-07-02
倩女手遊刀客魅者強控制(強混亂強眩暈強睡眠)和對應控制抗性的關系
 百态   2020-08-20
美國5月9日最新疫情:美國確診人數突破131萬
 百态   2020-05-09
荷蘭政府宣布將集體辭職
 干货   2020-04-30
倩女幽魂手遊師徒任務情義春秋猜成語答案逍遙觀:鵬程萬裏
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案神機營:射石飲羽
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案昆侖山:拔刀相助
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案天工閣:鬼斧神工
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案絲路古道:單槍匹馬
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案鎮郊荒野:與虎謀皮
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案鎮郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案鎮郊荒野:指鹿為馬
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案金陵:小鳥依人
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案金陵:千金買鄰
 干货   2019-11-12
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有