第八章 PHP 5中处理XML
时间:2008-01-18
来源:互联网

作者: 习明 发布时间: 2008-01-18

作者: jfcat 发布时间: 2008-01-18
SimpleXML扩展,在PHP 5中默认开启,是处理XML最简单的方法。你不需要记住负责的DOM API,只需要通过数据结构的形式访问XML。这里是它的4个简单的规则:
1. 属性表示元素的迭代器。
2. 数字索引表示元素。
3. 非数字索引表示属性。
4. 允许用字符串转换访问TEXT数据。
通过使用这4个规则,你可以访问XML文件中所有的数据。
8.4.1 创建一个SimpleXML对象 Creating a SimpleXML Object
你可以用3种方法中的任意一种创建一个SimpleXML对象,就像在这个例子中显示的一样:
PHP代码:
<?php
$sx1 = simplexml_load_file('example.xml');
$string = <<<XML
<?xml version='1.0'?>
<html xmlns="[url=http://www.w3.org/1999/xhtml]http://www.w3.org/1999/xhtml[/url]" xml:lang="en" lang="en">
<head>
<title>XML Example</title>
</head>
<body background="bg.png">
<p>
Moved to <a href="[url=http://example.org/]http://example.org/">example.org<a[/url]>.
</p>
<pre>
foo
</pre>
<p>
Moved to <a href="[url=http://example.org/]http://example.org/">example.org</a[/url]>.
</p>
</body>
</html>
XML;
$sx2 = simplexml_load_string($string);
$sx3 = simplexml_load_dom(new DomDocument());
?>
8.4.2 浏览SimpleXML对象 Browsing SimpleXML Objects
第一个规则是“属性表示元素的迭代器”,它表示你可以循环所有在<body>中的<p>标记,例如:
PHP代码:
<?php
foreach ($sx2->body->p as $p) {
}
?>
PHP代码:
<?php
$sx->body->p[1];
?>
PHP代码:
<?php
echo $sx->body['background'];
?>
PHP代码:
<?php
echo $sx->body->p[1];
?>
PHP代码:
<?php
echo strip_tags($sx->body->p[1]->asXML()) . "\n";
?>
PHP代码:
<?php
foreach ($sx->body->children() as $element) {
/* 对该元素进行一些操作 */
}
?>
PHP代码:
<?php
foreach ($sx->body->p[0]->a->attributes() as $attribute) {
echo $attribute . "\n";
}
?>
你可以保存一个修改的或者处理过的结构或者一个子节点到硬盘中。可以使用asXML()方法来实现,它可以让你在任何一个SimpleXML对象中调用:
PHP代码:
<?php
file_put_contents('filename.xml', $sx2->asXML());
?>
有些情况下,刚才所有的技术都不适合。例如,没有DOM XML扩展支持,或者你可能需要解析一些非常特殊的内容并且不想自己创建一个解析器。PEAR包含了可以处理有关解析XML的类,它们可能是有用的。我们将介绍其中的两种:XML_Tree和XML_RSS。XML_Tree可以用在没有DOM XML扩展支持时通过结构树搭建XML文档,或者在你不需要许多特性的情况下快速搭建一个文档时。XML_RSS可以解析RSS文件。RSS文件是用来描述最新条目的XML文档(例如)一个新闻站点。
8.5.1 XML_Tree
用XML_Tree创建一个XML文档是非常简单的,并且可以在没有DOM XML扩展支持的情况下实现。你可以通过在你的命令行界面中键入pear install XML_Tree来安装这个PEAR类。为了显示XML_Trees和“正常”的DOM XML方法之间的区别,我们将再次搭建相同的X(HT)ML 文档。
PHP代码:
<?php
require_once 'XML/Tree.php';
/* 创建文档和根节点 */
$dom = new XML_Tree;
$html =& $dom->addRoot('html', '',
array (
'xmlns' => 'http://www.w3.org/1999/xhtml',
'xml:lang' => 'en',
'lang' => 'en'
)
);
/* 创建head和title元素 */
$head =& $html->addChild('head');
$title =& $head->addChild('title', 'XML Example');
/* 创建body和p元素 */
$body =& $html->addChild('body', '', array ('background' =>
➥'bg.png'));
$p =& $body->addChild('p');
/* 增加"Moved to" */
$p->addChild(NULL, "Moved to ");
/* 增加a元素 */
$p->addChild('a', 'example.org', array ('href' =>
➥'http://example.org'));
/* 增加"."、br和"foo & bar" */
$p->addChild(NULL, ".");
$p->addChild('br');
$p->addChild(NULL, "foo & bar");
/* 输出显示的内容 */
$dom->dump();
?>
引用:
$p->addChild('a', 'example.org', array ('href' =>'http://example.org'));8.5.2 XML_RSS
RSS(RDF Site Summary,Really Simple Syndication)源是XML的一个常用应用。RSS是一个XML词汇表,用来描述新闻条目,并且这些新闻条目可以被集成(或者叫做内容聚合)到你自己的Web站点中。PHP.net有一个包含最近新闻条目的RSS源,地址是http://www.php.net/news.rss。你可以在http://web.resource.org/rss/1.0/spec中找到RSS规范的全部规则,但是查看一个例子会更好些。下面是我们即将解析的RSS文件的一部分:
PHP代码:
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns:rdf="[url=http://www.w3.org/1999/02/22-rdf-syntax-ns]http://www.w3.org/1999/02/22-rdf-syntax-ns[/url]#"
xmlns="[url=http://purl.org/rss/1.0/]http://purl.org/rss/1.0/[/url]"
xmlns:dc="[url=http://purl.org/dc/elements/1.1/]http://purl.org/dc/elements/1.1/[/url]"
>
<channel rdf:about="[url=http://www.php.net/]http://www.php.net/[/url]">
<title>PHP: Hypertext Preprocessor</title>
<link>[url]http://www.php.net/<[/url];/link>
<description>The PHP scripting language web site</description>
<items>
<rdf:Seq>
<rdf:li rdf:resource="[url=http://qa.php.net/]http://qa.php.net/[/url]" />
<rdf:li rdf:resource="[url=http://php.net/downloads.php]http://php.net/downloads.php[/url]" />
</rdf:Seq>
</items>
</channel>
<!-- RSS-Items -->
<item rdf:about="[url=http://qa.php.net/]http://qa.php.net/[/url]">
<title>PHP 4.3.5RC1 released!</title>
<link>[url]http://qa.php.net/<[/url];/link>
<description>PHP 4.3.5RC1 has been released for testing. This is the first release candidate and should have a very low number of problems and/or bugs. Nevertheless, please download and test it as much as possible on real-life applications to uncover any remaining issues. List of changes can be found in the NEWS file.</description>
<dc:date>2004-01-12</dc:date>
</item>
<item rdf:about="[url=http://www.php.net/downloads.php]http://www.php.net/downloads.php[/url]">
<title>PHP 5.0 Beta 3 released!</title>
<link>[url]http://www.php.net/downloads.php<[/url];/link>
<description>PHP 5.0 Beta 3 has been released. The third beta of PHP is also scheduled to be the last one (barring unexpectedsurprises). This beta incorporates dozens of bug fixes since Beta 2, better XML support and many other improvements, some of which are documented in the ChangeLog. Some of the key features of PHP 5 include: PHP 5 features the Zend Engine 2. XML support has been completely redone in PHP 5, all extensions are now focused ound the excellent libxml2 library ([url=http://www.xmlsoft.org/]http://www.xmlsoft.org/[/url]). SQLite has been bundled with PHP. For more information on SQLite, please visit their website. A new SimpleXML extension for easily accessing and manipulating XML as PHP objects. It can also interface with the DOM extension and vice-versa. Streams have been greatly improved, including the ability to access low-level socket operations on streams.<description><dc:date>2003-12-21<dc:date>
</item>
<!-- / RSS-Items PHP/RSS -->
</rdf:RDF>
我们将使用通过pear install XML_RSS安装的PEAR::XML_RSS类。下面是该脚本:
PHP代码:
<?php
require_once "XML/RSS.php";
$cache_file = "/tmp/php.net.rss";
首先,就跟刚才显示的一样,我们包含该PEAR类并且定义缓存文件的路径:
if (!file_exists($cache_file) ||
(filemtime($cache_file) < time() - 86400))
{
copy("[url=http://www.php.net/news.rss]http://www.php.net/news.rss[/url]", $cache_file);
}
接下来,我们检查文件是否已经被缓存并且缓冲的文件是否过期(一天是86 400秒)。如果文件不存在或者已经过期,我们将从php.net下载一个新的RSS文件并且把它存在缓冲文件当中:
$r =& new XML_RSS($cache_file);
$r->parse();
把XML_RSS类实例化以便解析RSS文件,然后调用parse()方法。这个方法将把RSS文件解析到一个数据结构中并可以让别的方法读取,例如getChannelInfo()将返回一个包含标题、描述和Web站点链接的数据,如下所示:
array(3) {
["title"]=>
string(27) "PHP: Hypertext Preprocessor"
["link"]=>
string(19) "[url=http://www.php.net/]http://www.php.net/[/url]"
["description"]=>
string(35) "The PHP scripting language web site"
}
getItems()返回的是新闻条目的标题、描述和链接。在下面的代码中,我们使用getItems()方法来遍历所有的条目并显示出来:
[php]foreach ($r->getItems() as $value) {
echo strtoupper($value['title']). "\n";
echo wordwrap($value['description']). "\n";
echo "\t{$value['link']}\n\n";
}
?>
PHP 4.3.5RC1 RELEASED!
PHP 4.3.5RC1 has been released for testing. This is the first release
candidate and should have a very low number of problems and/or bugs.
Nevertheless, please download and test it as much as possible on real-life
applications to uncover any remaining issues. List of changes can be found
in the NEWS file.
http://qa.php.net/
PHP 5.0 BETA 3 RELEASED!
PHP 5.0 Beta 3 has been released. The third beta of PHP is also
scheduled to be the last one (barring unexpected surprises). This
beta incorporates dozens of bug fixes since Beta 2, better XML
support and many other improvements, some of which are documented in
the ChangeLog. Some of the key features of PHP 5 include: PHP 5
features the Zend Engine 2. XML support has been completely redone in
PHP 5, all extensions are now focused around the excellent libxml2
library (http://www.xmlsoft.org/). SQLite has been bundled with PHP.
For more information on SQLite, please visit their website. A new
SimpleXML extension for easily accessing and manipulating XML as PHP
objects. It can also interface with the DOM extension and vice-versa.
Streams have been greatly improved, including the ability to access
low-level socket operations on streams.
http://www.php.net/downloads.php
作者: PHPChina 发布时间: 2008-01-18
作者: eclanp 发布时间: 2008-01-19

慢慢看~`
作者: 125231896 发布时间: 2008-01-20

作者: luzhou 发布时间: 2008-01-20

作者: blankyao 发布时间: 2008-01-21

作者: gvtbs 发布时间: 2008-01-21
作者: whchao001 发布时间: 2008-01-25
作者: wwwjyw8 发布时间: 2008-01-31

作者: luzhou 发布时间: 2008-01-31
支持。粗略的看了下
作者: liangjiao 发布时间: 2008-02-01
作者: taohui 发布时间: 2008-02-29
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28