top
Loading...
PHP和XML:使用expat函數(二)
讓我們看一下實際處理這個文檔的PHP代碼。
------------------------------------------------------------

<?php
/*NewsBoy : News system for the web written in PHP by Justin Grant (Web: jusgrant.cjb.net or justin.host.za.net Mail: justin@glendale.net)25 March V0.0.2 Converted Newsboy to a PHP class, allowing the layout to be easily modified. Also added made the HTML that is genrated a little easier to read.24 March V0.0.1 Just completed the intial version, very rough and basic.*/
class newsboy { var $xml_parser; var $xml_file; var $html; var $open_tag ; var $close_tag ;
//Class Constructor
function newsboy() { $this->xml_parser = ""; $this->xml_file = ""; $this->html = ""; $this->open_tag = array(
//these are the default settings but they are quite easy to modify
"NEWSBOY" => "<!-- XML Starts here --><TABLE COLS=1 CELLPADDING=5>", "STORY" => "<TR><TD BGCOLOR=#222222>", "DATE" => "<FONT COLOR=#BBBB00>", "SLUG" => "<FONT COLOR=#00AACC><B>  ", "TEXT" => "<FONT COLOR=#AAAAAA>", "PIC" => "", "NEWLINE" => "" ); $this->close_tag = array( "NEWSBOY" => "</TABLE><!-- XML Ends here -->", "STORY" => "</TD></TR>", "DATE" => "</FONT>", "SLUG" => "</B></FONT><BR>", "TEXT" => "</FONT>", "PIC" => "<IMAGE SRC=", "NEWLINE" => "<BR>" ); }
//Class Destructor (has to be invoked manually as PHP does not support destructors)

function destroy() { xml_parser_free($this->xml_parser); }
//Class Members
function concat($str) { $this->html .= $str; }
function startElement($parser, $name, $attrs) { //global $open_tag; if ($format= $this->open_tag[$name]) { $this->html .= $format; } }
function endElement($parser, $name) { global $close_tag; if ($format= $this->close_tag[$name]) { $this->html .= $format; } }
function characterData($parser, $data) { $this->html .= $data; }
/* function PIHandler($parser, $target, $data) { //switch (strtolower($target)){ // case "php": eval($data); // break; //} }*/
function parse() { $this->xml_parser = xml_parser_create(); xml_set_object($this->xml_parser, &$this); // use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, true); xml_set_element_handler($this->xml_parser, "startElement", "endElement"); xml_set_character_data_handler($this->xml_parser, "characterData");//xml_set_processing_instruction_handler($this->xml_parser, "PIHandler");
if (!($fp = fopen($this->xml_file, "r"))) { die("could not open XML input"); } while ($data = fread($fp, 4096)) { if (!xml_parse($this->xml_parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->xml_parser)), xml_get_current_line_number($this->xml_parser))); } } }}
?>

--------------------------------------------------------------------------------

在這個類的構造函數中,我創建了打開與關閉兩個標記數組。數組的關鍵字與我后面將要分析的標記是
一樣的,并且它們相應的值包含格式化打開與關閉標記的HTML代碼。

我定義了一個簡單的類析構函數用來當我們不再需要它時釋放XML 分析器。這個函數不得不手工調用,
因為PHP不支持當一個對象釋放時自動調用類的析構函數。

然后我定義了在XML文檔中用來分析打開和關閉標記的主回調方法。我也定義了一個數據分析方法, 將
用于當打開和關閉標記中有數據時,對數據進行簡單的格式化,后面我將向你演示如何將這些回調方法注冊
到分析器中。

在startElement和closeElement(當分析到一個打開或關閉標專時被分別調用)中使用 標記的名字
作為索引鍵值對相應的數組進行查詢。如果那個鍵值存在,則返回值并且追加到類的'html' 屬性的后面。
'html'屬性將在以后我們真正顯示文檔內容的時候使用。

characterData方法簡單地將標記之間的值加到類的html屬性的后面。

被注釋起來的叫PIHandler的方法是一個回調函數,我還未曾實現它。如果它存在的話,它將直接在XML
文檔中處理php腳本。

現在,讓我解釋一下主要的分析方法的調用,你猜一猜,parse()!!!

第一行調用了函數xml_parser_create(),它將返回一個expat的xml分析器的實例,并且被保存在類的
屬性&this->xml_parser中。

下一步,我們需要用函數xml_set_object()來注冊一個類方法的回調函數。

我是這樣使用的,xml_set_object($this->xml_parser, &$this)。我在第一個參數中指定了用
來保存xml 分析器的類屬性,然后在第二個參數,我指定了PHP對象的實例地址。這個可以讓分析器 知道全
部將要注冊的回調函數,是在那個地址上指定類的實際的方法。這就象c或c++中的一個'引用傳遞',也有人
簡單地叫做'引用變量'。

在下一行,我調用了xml_parser_set_option()設置了一個xml分析器的屬性,使用大小寫折疊( case
folding)。大小寫折疊只是告訴分析器知道,當我分析我的XML文檔時我并不關心大小寫敏感,但是 如果你
想使用大小寫敏感來定義兩個不同的標記,如<Story>或<story>,你可以不設置它。

通過使用xml_set_element_handler(),我指定了用于開始和結束標記的回調函數,名字是
"startElement"和"endElement"。

接著,我使用xml_set_character_data_handler()來指定字符數據的處理句柄為名為
characterData()的回調函數。被注釋的函數調用,xml_set_processing_instruction_handler(),
是一個我用于注冊函數 PIHandler()的調用。PIHandler可以被包括在XML文檔中處理php代碼。

其它的代碼只是很簡單地讀XML文件并且分析它。如果一個錯誤發生,那么錯誤明細將返回,包括錯誤
發生的行號。

北斗有巢氏 有巢氏北斗