首頁  >  文章  >  後端開發  >  PHP 和 XML: 使用expat函數(一)

PHP 和 XML: 使用expat函數(一)

黄舟
黄舟原創
2016-12-19 13:22:05891瀏覽

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 !) . 
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 學 XML doc is aned s (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 ?
03/25/2000 
NewsBoy Class 

Converted Newsboy to a PHP class to allow better abstraction (as far as PHP allows.) 

..a that means this is thatversion oon on how to use and customize the class. 





03/24/2000 
NewsBoy is up! 

I have just finishedBoy v.0505" 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 formating use of XML 
for¢ built in expat parser for Apache. 
This is just the very first version and there will be loads of improvements as the 
project progresses. 

o🜎
project progresses. 
🎎 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' feffect that cur.
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 ball to p . 
The official website for the movie is > here  


romeomustdie.net/images/image15.jpg" width="300">ML🠎使用expat函數(一)的內容,更多相關內容請關注PHP中文網(www.php.cn)! 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn