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 相关文章推荐
Windows下PHP的任意文件执行漏洞
Oct 09 PHP
PHP的开发框架的现状和展望
Mar 16 PHP
从Web查询数据库之PHP与MySQL篇
Sep 25 PHP
php的array_multisort()使用方法介绍
May 16 PHP
php四种基础算法代码实例
Oct 29 PHP
Laravel框架中实现使用阿里云ACE缓存服务
Feb 10 PHP
初识laravel5
Mar 02 PHP
Apache服务器下防止图片盗链的办法
Jul 06 PHP
PHP手机号中间四位用星号*代替显示的实例
Jun 02 PHP
PHP基于IMAP收取邮件的方法示例
Aug 07 PHP
laravel实现上传图片的两种方式小结
Oct 12 PHP
Laravel框架实现定时Task Scheduling例子
Oct 22 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
php项目打包方法
2008/02/18 PHP
PHP批量采集下载美女图片的实现代码
2013/06/03 PHP
Yii实现的多级联动下拉菜单
2016/07/13 PHP
PHP实现一个简单url路由功能实例
2016/11/05 PHP
改版了网上的一个js操作userdata
2007/04/27 Javascript
node.js中的http.response.removeHeader方法使用说明
2014/12/14 Javascript
javascript实现日期格式转换
2014/12/16 Javascript
node.js中的fs.closeSync方法使用说明
2014/12/17 Javascript
jQuery插件简单实现方法
2015/07/18 Javascript
利用JQuery阻止事件冒泡
2016/12/01 Javascript
webpack入门+react环境配置
2017/02/08 Javascript
EasyUI的DataGrid每行数据添加操作按钮的实现代码
2017/08/22 Javascript
原生JS与jQuery编写简单选项卡
2017/10/30 jQuery
JavaScript学习总结(一) ECMAScript、BOM、DOM(核心、浏览器对象模型与文档对象模型)
2018/01/07 Javascript
jQuery实现的淡入淡出与滑入滑出效果示例
2018/04/18 jQuery
Vue动态路由缓存不相互影响的解决办法
2019/02/19 Javascript
JavaScript Canvas编写炫彩的网页时钟
2019/10/16 Javascript
vue学习笔记之给组件绑定原生事件操作示例
2020/02/27 Javascript
通过实例解析javascript Date对象属性及方法
2020/11/04 Javascript
python下函数参数的传递(参数带星号的说明)
2010/09/19 Python
python实现爬虫下载漫画示例
2014/02/16 Python
Python实现把xml或xsl转换为html格式
2015/04/08 Python
python实现图片处理和特征提取详解
2017/11/13 Python
利用Python批量提取Win10锁屏壁纸实战教程
2018/03/27 Python
python2.x实现人民币转大写人民币
2018/06/20 Python
详解Django解决ajax跨域访问问题
2018/08/24 Python
python 实现list或string按指定分段
2019/12/25 Python
python对文件的操作方法汇总
2020/02/28 Python
Python如何使用ElementTree解析xml
2020/10/12 Python
使用CSS3制作响应式导航菜单的方法
2015/07/12 HTML / CSS
中国首家奢侈品O2O网购平台:第五大道奢侈品网
2017/12/14 全球购物
写给女生的道歉信
2014/01/14 职场文书
暑期社会实践感言
2014/02/25 职场文书
新教师培训心得体会
2014/09/02 职场文书
CSS中Single Div 绘图技巧的实现
2021/06/18 HTML / CSS
python中validators库的使用方法详解
2022/09/23 Python