PHP5的Simplexml

php5新增了Simplexml extension,我们可以借助它来解析,修改XML。在IBM的知识库里找到一篇文章对此做了专门的介绍,而且比较详细,感兴趣的话可以看看最后的参考文档。

一个RSS Feed

下面是一个RSS的例子,我们准备用simplexml来解析它。

XML:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <rss version="0.92">
  3. <channel>
  4.   <title>Mokka mit Schlag</title>
  5.   <link>http://www.elharo.com/blog</link>
  6.   <language>en</language>
  7.   <item>
  8.     <title>Penn Station: Gone but not Forgotten</title>
  9.     <description>
  10.      The old Penn Station in New York was torn down before I was born.
  11.      Looking at these pictures, that feels like a mistake.  The current site is
  12.      functional, but no more; really just some office towers and underground
  13.      corridors of no particular interest or beauty. The new Madison Square...
  14.     </description>
  15.     <link>http://www.elharo.com/blog/new-york/2006/07/31/penn-station</link>
  16.   </item>
  17.   <item>
  18.     <title>Personal for Elliotte Harold</title>
  19.     <description>Some people use very obnoxious spam filters that require you
  20.      to type some random string in your subject such as E37T to get through.
  21.      Needless to say neither I nor most other people bother to communicate with
  22.      these paranoids. They are grossly overreacting to the spam problem.
  23.      Personally I won't ...</description>
  24.  
  25.     <link>http://www.elharo.com/blog/tech/2006/07/28/personal-for-elliotte-harold/</link>
  26.   </item>
  27. </channel>
  28. </rss>

解析XML

首先载入一个xml

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

这里使用的是simplexml_load_file函数,能够马上解析指定url的xml文件,因为是simplexml,所以simple。下面就可以象读取php数组一样来使用解析后xml的内容了,比如读取RSS的标题:

PHP:
  1. $title$rss->channel->title;
  2. <title><?php echo $title; ?></title>

或者是循环显示rss的各个ITEM节点

PHP:
  1. $rss->channel->item //这个是item

PHP:
  1. foreach ($rss->channel->item as $item) {
  2.   echo "<h2>" . $item->title . "</h2>";
  3.   echo "<p>" . $item->description . "</p>";
  4. }

一个简单但完整的RSS Reader

把上面的代码整合在一起,就是一个五脏俱全的麻雀牌RSS Reader了。

PHP:
  1. <?php
  2. // 载入并解析XML
  3. $rss =  simplexml_load_file('http://partners.userland.com/nytRss/nytHomepage.xml');
  4. $title$rss->channel->title;
  5. ?>
  6. <html xml:lang="en" lang="en">
  7. <head>
  8.   <title><?php echo $title; ?></title>
  9. </head>
  10. <body>
  11.  
  12. <h1><?php echo $title; ?></h1>
  13.  
  14. <?php
  15. // 循环输出ITEM节点的说明
  16. foreach ($rss->channel->item as $item) {
  17.   echo "<h2><a href='" . $item->link . "'>" . $item->title . "</a></h2>";
  18.   echo "<p>" . $item->description . "</p>";
  19. }
  20. ?>
  21.  
  22. </body>
  23. </html>

Simplexml,真的很simple,不信可以拿去和php的DOM function做下比较:)

参考

http://www-128.ibm.com/developerworks/xml/library/x-simplexml.html

作者: Volcano 发表于October 12, 2006 at 7:37 am

版权信息: 可以任意转载, 转载时请务必以超链接形式标明文章原始出处作者信息及此声明

Tags: ,,

6 条评论 »

  1. Leonardo 于 2006-10-12 @ 13:38:07 留言

    看起来确实不错,简单易用。

  2. Binlle 于 2006-10-12 @ 17:34:25 留言

    严禁楼上的打广告.学术重地,不容撒野。楼主只研究PHP吗?

  3. volcano 于 2006-10-12 @ 18:05:40 留言

    工作原因,比较偏重php这方面,其它方面正在逐渐涉猎

  4. MP3讯雷 于 2007-05-22 @ 17:09:31 留言

    首先不能设置HEADER头

    其次不能指定读一小段,比如我只想读第一个item但它还是把文件都抓回来了

  5. volcano 于 2007-05-22 @ 18:32:26 留言

    在这方面,simplexml不会做的那么细致的
    你可以考虑先用其它程序将xml下回来再做进一步解析,比如pear::http_request

  6. winnie 于 2008-11-04 @ 00:01:49 留言

    看到大大的这篇文章茅舍顿开,另外想请教一下,如果想读取多个rss源应该怎么处理。

RSS 为此帖反馈评论 · 反向跟踪 网站

留条评论