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 相关文章推荐
聊天室php&amp;mysql(六)
Oct 09 PHP
php中static静态变量的使用方法详解
Jun 04 PHP
PHP连接SQLServer2005 的问题解决方法
Jul 19 PHP
PHP生成唯一的促销/优惠/折扣码(附源码)
Dec 28 PHP
phpexcel导入excel数据使用方法实例
Dec 24 PHP
Yii实现MySQL多数据库和读写分离实例分析
Dec 03 PHP
php制作的简单验证码识别代码
Jan 26 PHP
PHP将二维数组某一个字段相同的数组合并起来的方法
Feb 26 PHP
php  PATH_SEPARATOR判断当前服务器系统类型实例
Oct 28 PHP
PHP-FPM 设置多pool及配置文件重写操作示例
Oct 02 PHP
Laravel 修改默认日志文件名称和位置的例子
Oct 17 PHP
YII2框架中behavior行为的理解与使用方法示例
Mar 13 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 ignore_user_abort与register_shutdown_function 使用方法
2009/06/14 PHP
php 小乘法表实现代码
2009/07/16 PHP
php遍历数组的4种方法总结
2014/07/05 PHP
PHP实现HTML生成PDF文件的方法
2014/11/07 PHP
PHP常用设计模式之委托设计模式
2016/02/13 PHP
php函数传值的引用传递注意事项分析
2016/06/25 PHP
PHP 配置后台登录以及模板引入
2017/01/24 PHP
js 自定义的联动下拉框
2010/02/07 Javascript
function foo的原型与prototype属性解惑
2010/11/19 Javascript
jQuery 插件仿百度搜索框智能提示(带Value值)
2013/01/22 Javascript
js脚本获取webform服务器控件的方法
2014/05/16 Javascript
浅析node连接数据库(express+mysql)
2015/11/30 Javascript
7个jQuery最佳实践
2016/01/12 Javascript
基于BootStrap Metronic开发框架经验小结【五】Bootstrap File Input文件上传插件的用法详解
2016/05/12 Javascript
Bootstrap菜单按钮及导航实例解析
2016/09/09 Javascript
浅析javascript中的Event事件
2016/12/09 Javascript
使用canvas及js简单生成验证码方法
2017/04/02 Javascript
Angular6 正则表达式允许输入部分中文字符
2018/09/10 Javascript
深入解析koa之中间件流程控制
2019/06/17 Javascript
layui自定义插件citySelect实现省市区三级联动选择
2019/07/26 Javascript
python3编写C/S网络程序实例教程
2014/08/25 Python
TensorFlow神经网络优化策略学习
2018/03/09 Python
Python3爬虫爬取百姓网列表并保存为json功能示例【基于request、lxml和json模块】
2018/12/05 Python
解决pycharm回车之后不能换行或不能缩进的问题
2019/01/16 Python
基于python实现百度翻译功能
2019/05/09 Python
Python Collatz序列实现过程解析
2019/10/12 Python
Tensorflow 实现释放内存
2020/02/03 Python
经验丰富程序员才知道的8种高级Python技巧
2020/07/27 Python
如何将字串String转换成整数int
2015/02/21 面试题
应用化学专业职业生涯规划书
2013/12/31 职场文书
文明市民先进事迹
2014/05/15 职场文书
2014年财政工作总结
2014/12/10 职场文书
看古人们是如何赞美老师的?
2019/07/08 职场文书
一篇合格的广告文案,其主要目的是什么?
2019/07/12 职场文书
教您:房贷工资收入证明应该怎么写?
2019/08/19 职场文书
如何搭建 MySQL 高可用高性能集群
2021/06/21 MySQL