linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘建议留言网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > article > Linux开发区 > WEB开发 >
栏目导栏
资料搜索
热门文章
·JSP三种页面跳转方式的比较
·LOG4J快速入门及参考资料
·Javascript设计网页中的下拉菜
·什么是PHP
·Win2003下APACHE+PHP5+MYSQL4+
·apache 的httpd.conf 文件
·MQSeries 入门手册
·php上传文件大小限制大全
·solaris 10 安装jsp大全
·半小时教你学会正则表达式
·[硬件 通讯]PHP使用RS232串口通
·如何利用JBuilder2005开发Web应
·网页技巧二十例
·JSP应用语法大全
·Linux下jsp开发环境,apache,t
最新文章
·通过Apache访问svn代码库的配置
·apache 进程数与PHP的加密
·Linux系统上WebLogic安装调试的
·在Fedora8系统下搭建JSP开发环
·Web开发设计的五大准则
·使用PHP开发SCA和SDO
·用rsync实现网站镜像和备份
·能得到你是从什么页面过来的,
·使用PHP批量生成随机用户名
·linux下编译php的配置参数总结
·在debian下为PHP5.0.3安装pdo模
·apache为用户开启主页服务
·Linux下常用php编辑工具
·java在Linux下的web应用
·面向站长和网站管理员的Web缓存
Google
 
PHP的XML分析函数(二)
[ 作者:  加入时间:2005-11-24 21:03:24  来自: ]
 可扩展标识语言(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 

<?xml version="1.0" standalone="no"?> 
<!DOCTYPE NewsBoy SYSTEM "NewsBoy.dtd"> 

<NewsBoy> 

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

<story> 
<date>03/25/2000</date> 
<slug>NewsBoy Class</slug> 
<text> 
Converted Newsboy to a PHP class to allow better abstraction (as far 
as PHP allows.) <newline></newline> 
Guess that means this is version 0.02 ?!<newline></newline> 
Newsboy will have a section of it's own soon on how to use and 
customize the class. <newline></newline> 
</text> 
</story> 

<story> 
<date>03/24/2000</date> 
<slug>NewsBoy is up!</slug> 
<text> 
I have just finished NewsBoy v0.01 !!! <newline></newline> 
It looks quite promising. You may ask, ""What the heck is it?!
".<newline> </newline> 
Well it's a simple news system for web-sites, written in PHP, 
that makes use of XML 
for <newline></newline> 
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. 
</text> 
</story> 

<story> 
<date>03/24/2000</date> 
<slug>Romeo must Die</slug> 
<text> 
Saw a really cool movie today at Mann called 'Romeo must Die' 
<newline></newline> 
Nice fight scenes for a typical kung-fu movie with some 'Matrix' 
style effects.<newline> 
</newline> 
One particular cool effect was the 'X-Ray Vision' effect that 
occured in various 
fight scenes. <newline></newline> 
The hero, played by Jet Li, strikes a bad guy and you can see 
the bone in his arm 
crack, in X-RAY vision. <newline></newline> 
There were some funny scenes too when Jet has to play American
football with the 
bad guys. <newline></newline> 
The official website for the movie is
<A HREF='http://www.romeo-must-die.com' 
> here </A> <newline></newline> 
<newline></newline> 
</text> 
<IMG SRC="http://a1996.g.akamaitech.net/7/1996/25/e586077a88e7a4/ 
romeomustdie.net/images/image15.jpg" WIDTH=300 > 
</story> 

</newsboy> 

  译者注:上面的代码中为了排版,我作了换行处理。

  好,如果你对XML文档不是很熟悉的话,那么这个看上去可能有一点令人吃惊,相当的不好理解。第一行 是一个XML的声明。'version'属性告诉分析器这篇文档是遵守W3C所定义的XML 1.0标准的。'standalone' 选 项表示分析这篇文档的分析器需要一个DTD定义来验证XML文档(在这个例子中,DTD存在于一个存立的 文件中, 名字是'NewsBoy.dtd',就是通过下一行DOCTYPE声明所指定的,但是如果你愿意你也可以在同 一个文档中来 定义它)。DOCTYPE声明指出了XML文档的根元素,在这个例子中是'NewsBoy'元素。同时它也 指出了DTD与XML 文档存在于同一目录下。请注意,我没有验证XML文档所对应的DTD,因为expat 不能验证一个XML文档。根据expat的作者James Clark所说,原因是这个分析器是同W3C关于XML的分析器 的说明书一致的,那里面的分析 器不需要验证文档的有效性,但是程序员应该去处理它。

  后面的部分就是包括了按我定义的NewsBoy类格式所建立的故事了。

Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
无相关信息