PHP 中的 SimpleXML 处理
了解和 php 版本 5 捆绑到一起的 simplexml 扩展,它使 php 页面能够以 php 友好的语法来查询、搜索、修改和重新发布 xml。
php 版本 5 引入了 simplexml,一种用于读写 xml 的新的应用程序编程接口(api)。在 simplexml 中,下面的这样的表达式:
$doc->rss->channel->item->title
从文档中选择元素。只要熟悉文档的结构,很容易编写这种表达式。但是,如果不很清楚需要的元素出现在何处(比如 docbook、html 和类似的叙述性文档中),simplexml 可以使用 xpath 表达式寻找这些元素。
开始使用 simplexml
假设需要一个 php 页面将 rss 提要(feed)转化成 html。rss 是一种简单的 xml 格式用于发布连锁内容。文档的根元素是 rss,它包括一个 channel 元素。channel 元素包含关于提要的元数据,如标题、语言和 url。它还包含各种封装在 item 元素中的报道。每个 item 都有一个 link 元素,包括一个 url,还有 title 或 description(通常两者都有),包含普通文本。不使用名称空间。rss 的内容当然不止这些,不过对本文来说知道这些就足够了。清单 1 显示了一个典型的例子,它包含两个新闻项。
清单 1. rss 提要
<?xml version="1.0" encoding="utf-8"?>
<rss version="0.92">
<channel>
<title>mokka mit schlag</title>
<link>http://www.elharo.com/blog</link>
<language>en</language>(来源 www.iocblog.net)
<item>
<title>penn station: gone but not forgotten</title>
<description>
the old penn station in new york was torn down before i was born.
looking at these pictures, that feels like a mistake. the current site is
functional, but no more; really just some office towers and underground
corridors of no particular interest or beauty. the new madison square...
</description>
<link>http://www.elharo.com/blog/new-york/2006/07/31/penn-station</link>
</item>
<item>
<title>personal for elliotte harold</title>
<description>some people use very obnoxious spam filters that require you
to type some random string in your subject such as e37t to get through.
needless to say neither i nor most other people bother to communicate with
these paranoids. they are grossly overreacting to the spam problem.
personally i won't ...</description>
<link>http://www.elharo.com/blog/tech/2006/07/28/personal-for-elliotte-harold/</link>
</item>
</channel>
</rss>
我们来开发一个 php 页面将 rss 提要格式化为 html。清单 2 显示了这个页面的基本结构。
清单 2. php 代码的静态结构
<?php // load and parse the xml document ?>
<html xml:lang="en" lang="en">
<head>
<title><?php // the title will be read from the rss ?></title>
</head>
<body>
<h1><?php // the title will be read from the rss again ?></h1>
<?php
// here we'll put a loop to include each item's title and description
?>
</body>
</html>
解析 xml 文档
第一步是解析 xml 文档并保存到变量中。只需要一行代码,向 simplexml_load_file() 函数传递一个 url 即可:
$rss = simplexml_load_file('http://partners.userland.com/nytrss/nythomepage.xml');
对于这个例子,我已经从 userland 的 new york times 提要(在 http://partners.userland.com/nytrss/nythomepage.xml)填充了页面。当然,也可使用其他 rss 提要的任何 url。
要注意,虽然名称为 simplexml_load_file(),该函数实际上解析远程 http url 上的 xml 文档。但这并不是该函数唯一令人感到奇怪的地方。返回值(这里存储在 $rss 变量中)并没有指向整个文档,如果使用过其他 api 如文档对象模型(dom)您可能会这样期望。相反,它指向文档的根元素。从 simplexml 不能访问文档序言和结语部分的内容。
寻找提要标题
整个提要的标题(不是提要中各报道的标题)位于 rss 根元素 channel 的 title 孩子中。很容易找到这个标题,就仿佛 xml 文档是类 rss 的一个对象的序列化形式,它的 channel 字段本身带有一个 title 字段。使用常规 php 对象引用语法,寻找标题的语句如下:
$title = $rss->channel->title;
找到之后可以将其添加到输出 html 中。这样做很简单,只要回显 $title 变量即可:
<title><?php echo $title; ?></title> (来源 www.iocblog.net)
这一行输出元素的字符串值而不是整个元素。就是说写入文本内容但不包括标签。
甚至可以完全跳过中间变量 $title:
<title><?php echo $rss->channel->title; ?></title>
因为该页面在多处重用这个值,我发现用一个含义明确的变量来存储会更方便。
……
Tag: SimpleXML
文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。