top
Loading...
PHP和XML:使用expat函數(一)
譯者:limodou 可擴展標識語言(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類格式所建立的故事了。

轉自PHPBuilder.com

北斗有巢氏 有巢氏北斗