PHP 和 XML: 使用expat函数(一)


Posted in PHP onOctober 09, 2006

PHP 和 XML: 使用expat函数(一)

可扩展标识语言(Extensible Markup Language )明显是大多数开发人员将想要将其加入到他们的工具箱中的东西。XML是一种W3C的标准,它是开放的,语言中性的,API中性的,流式的,文本的,人类可读 的, 并且是一种将结构化数据带到web上的一种方法。XML是SGML的一个子集,它本身并不是一种标识 语言,但是 它允许作者来定义他们自已的标识语言,以便同分级数据保持更好的一致性。

现在,用PHP 分析XML文档已经不是一个象我以前在web和其它地方所看到的被覆盖得很深的专题了。 在 PHP手册中已经提供了一些对XML分析函数非常有用的信息,但是这个看上去好象就是我所能找到的 全部的信 息了。其它的语言看上去比PHP已经有了更多的关于XML的信息和工作实例,所以在这篇文章 中,我将试图为 改变这种情况作出我的一部分努力。

我将带领读者体验一个相当简单的XML的应用,那个应用是为我的网站所做的新闻系统的实现。我确实在 我的网站使用了这个应用,现在它工作的很好。如果你喜欢你可以自由地使用它。好了,让我们开始吧!

为了在PHP中使XML分析函数有效,你需要一个支持XML 的模块在你的web服务器上。这就意味着你将可能 不得不重新编译你的模块,以便可以支持XML,请参考这里来查看如何做到的更多的信息。XML 分析函数现在 真正地包含在一种SAX分析器expat中,它提供了 关于XML的简单的函数。另一种分析器是DOM分析器,它更容 易使用,关于它的一个例子就是微软的MSXML分析器组件,它可以让程序员通过操纵一种树状样式的对象来处 理结点和元素。expat分析器(或任意的SAX 分析器)允许你分析一个XML文档的实现方法是在对XML文档进行分 析的时候对不同的标记类型指定回调函数来完成的。当分析器开始分析你的XML文档并且遇上了一个标记,它 将调用你的函数,并且在继续往下 执行之前由你的函数对特定的标记进行处理。你可以把它看作是一种事件 驱动的方法。

让我们看一个使用'Newsboy'类来分析的XML文档

--------------------------------------------------------------------------------
mynews.xml

03/31/2000
Sooo Busy !

I haven't posted anything here for a while now as I have been busy with work(have to pay those
bills!).
I have just finished a neat little script that stores a complete record set in a session
variable after
doing an SQL query. The neat part is that an XML doc is stored in the session variable
an when paging
through the results (often near 1000!) the script displays 50 results at a time from the
XML doc in the
session variable instead of doing another query against the database. It takes a BIG load
off of the
database server.

03/25/2000
NewsBoy Class

Converted Newsboy to a PHP class to allow better abstraction (as far as PHP allows.)

Guess that means this is version 0.02 ?!
Newsboy will have a section of it's own soon on how to use and customize the class.

03/24/2000
NewsBoy is up!

I have just finished NewsBoy v0.01 !!!
It looks quite promising. You may ask, ""What the heck is it?!".

Well it's a simple news system for web-sites, written in PHP, that makes use of XML
for
the news data format allowing easy updating and portability between platforms.
It uses the built in expat parser for Apache.
This is just the very first version and there will be loads of improvements as the
project progresses.

03/24/2000
Romeo must Die

Saw a really cool movie today at Mann called 'Romeo must Die'
Nice fight scenes for a typical kung-fu movie with some 'Matrix' style effects.

One particular cool effect was the 'X-Ray Vision' effect that occured in various
fight scenes.
The hero, played by Jet Li, strikes a bad guy and you can see the bone in his arm
crack, in X-RAY vision.
There were some funny scenes too when Jet has to play American football with the
bad guys.
The official website for the movie is <A HREF='http://www.romeo-must-die.com'
> here </A>

<IMG SRC="http://a1996.g.akamaitech.net/7/1996/25/e586077a88e7a4/
romeomustdie.net/images/image15.jpg" WIDTH=300 >

PHP 相关文章推荐
ThinkPHP3.1新特性之动态设置自动完成及自动验证示例代码
Jun 23 PHP
Drupal简体中文语言包安装教程
Sep 27 PHP
php中addslashes函数与sql防注入
Nov 17 PHP
浅析php单例模式
Nov 25 PHP
帝国cms目录结构分享
Jul 06 PHP
PHP实现事件机制的方法
Jul 10 PHP
WordPress中限制非管理员用户在文章后只能评论一次
Dec 31 PHP
PHP将MySQL的查询结果转换为数组并用where拼接的示例
May 13 PHP
php 从一个数组中随机的取出若干个不同的数实例
Dec 31 PHP
PHP微信公众号开发之微信红包实现方法分析
Jul 14 PHP
php检测mysql表是否存在的方法小结
Jul 20 PHP
php服务器的系统详解
Oct 12 PHP
用PHP动态生成虚拟现实VRML网页
Oct 09 #PHP
其他功能
Oct 09 #PHP
PHP新手上路(十一)
Oct 09 #PHP
PHP新手上路(十二)
Oct 09 #PHP
PHP新手上路(十三)
Oct 09 #PHP
PHP新手上路(十四)
Oct 09 #PHP
PHP简介
Oct 09 #PHP
You might like
[EPIC] Larva vs Flash ZvT @ Crossing Field [2017-10-09]
2020/03/17 星际争霸
php实现图形显示Ip地址的代码及注释
2014/01/20 PHP
php匹配字符中链接地址的方法
2014/12/22 PHP
PHP中快速生成随机密码的几种方式
2017/04/17 PHP
Laravel学习教程之View模块详解
2017/09/18 PHP
PHP实现微信公众号验证Token的示例代码
2019/12/16 PHP
字符串的replace方法应用浅析
2011/12/06 Javascript
解析瀑布流布局:JS+绝对定位的实现
2013/05/08 Javascript
动态加载js、css等文件跨iframe实现
2014/02/24 Javascript
jquery easyui 对于开始时间小于结束时间的判断示例
2014/03/22 Javascript
node.js中的fs.utimes方法使用说明
2014/12/15 Javascript
jquery渐隐渐显的图片幻灯闪烁切换实现方法
2015/02/26 Javascript
jQuery实现tab选项卡效果的方法
2015/07/08 Javascript
js获取url传值的方法
2015/12/18 Javascript
javascript HTML5 canvas实现打砖块游戏
2020/06/18 Javascript
Bootstrap三种表单布局的使用方法
2016/06/21 Javascript
JS实现颜色动态淡化效果
2017/03/06 Javascript
详解vue项目中如何引入全局sass/less变量、function、mixin
2018/06/02 Javascript
[01:08:10]2014 DOTA2国际邀请赛中国区预选赛 SPD-GAMING VS LGD-CDEC
2014/05/22 DOTA
[03:38]TI4西雅图DOTA2前线报道 71专访
2014/07/08 DOTA
python3使用smtplib实现发送邮件功能
2018/05/22 Python
Python多重继承的方法解析执行顺序实例分析
2018/05/26 Python
python对象转字典的两种实现方式示例
2019/11/07 Python
解决pycharm 安装numpy失败的问题
2019/12/05 Python
AC Lens:购买隐形眼镜
2017/02/26 全球购物
英国最大的纸工艺品商店:CraftStash
2018/12/01 全球购物
Yahoo-PHP面试题3
2012/01/14 面试题
中科创达面试题
2016/12/28 面试题
部队万能检讨书
2014/02/20 职场文书
捐赠仪式主持词
2014/03/19 职场文书
三峡大坝导游词
2015/01/31 职场文书
感恩教育观后感
2015/06/17 职场文书
Mysql Show Profile
2021/04/05 MySQL
解决goland 导入项目后import里的包报红问题
2021/05/06 Golang
Nginx内网单机反向代理的实现
2021/11/07 Servers
Java 中的 Lambda List 转 Map 的多种方法详解
2022/07/07 Java/Android