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.      <descrīption>
  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.      </descrīption>
  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.      <descrīption>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 ... </descrīption>
  24.      <link>http://www.elharo.com/blog/tech/2006/07/28/personal-for-elliotte-harold/ </link>
  25.    </item>
  26. </channel>
  27. </rss>

解析XML

首先载入一个xml

PHP:

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

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

PHP:
    • $title =   $rss-> channel-> title;
    • <title><?php echo $title; ?></title>

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

PHP:
    • $rss-> channel-> item //这个是item
PHP:
    • foreach ( $rss-> channel-> item as $item ) {
    •    echo "<h2>" . $item-> title . "</h2>";
    •    echo "<p>" . $item-> descrīption . "</p>";
    • }

一个简单但完整的RSS Reader

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

PHP:
    • <?php
    • // 载入并解析XML
    • $rss =   simplexml_load_file ( 'http://partners.userland.com/nytRss/nytHomepage.xml' );
    • $title =   $rss-> channel-> title;
    • ?>
    • <html xml:lang= "en" lang= "en">
    • <head>
    •    <title><?php echo $title; ?></title>
    • </head>
    • <body>
    • <h1><?php echo $title; ?></h1>
    • <?php
    • // 循环输出ITEM节点的说明
    • foreach ( $rss-> channel-> item as $item ) {
    •    echo "<h2><a href='" . $item-> link . "'>" . $item-> title . "</a></h2>";
    •    echo "<p>" . $item-> descrīption . "</p>";
    • }
    • ?>
    • </body>
    • </html>

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

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值