PHP 和 XML: 使用expat函数(一)
2024-05-04 22:57:39
供稿:网友
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 >