您现在的位置: > 首页 > 网站建设 > 网页编程
>> 最新教程
>> 热门教程
>> 最新游戏资讯
>> 热门游戏资讯
用php5的simplexml解析各种feed
作者:本站                来自:安迪教程网                 加入时间:08-11-20                进入论坛讨论

  用simplexml处理atom数据

  很多博客使用atom来输出数据,但是atom使用了名称空间(namespace),所以现在请求被命名的元素和本地名称时必须指定名称空间统一资源标识符(URI),还有一点就是simplexml的xpath方法无法直接query这个xml tree。

  从 PHP 5.1 版开始,SimpleXML 可以直接对带名称空间的文档使用 XPath 查询。和通常一样,XPath 位置路径必须使用名称空间前缀,即使搜索的文档使用默认名称空间也仍然如此。registerXPathNamespace() 函数把前缀和后续查询中使用的名称空间 URL 联系在一起。

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

$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的例子:

$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->item as $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->subject as $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方法来获得名称空间中的数据:

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

  相关文章:

  怎样在PHP 中运用 SimpleXML 处理任务

  PHP SimpleXML 函数

  用PHP5的SimpleXML解析XML文档

  用php5的simplexml解析各种feed

  PHP中的SimpleXML处理

  php5的simplexml解析错误

联系方式:QQ:6084884 email:agwcn@126.com 粤ICP备05055782号
本网站中发布的文章只代表发表人的个人观点,不代表安迪教程网网站的立场
copyright© 2000-2004 安迪教程网 All rights reserved agwcn.com