文章詳情頁(yè)
PHP 和 XML: 使用expat函數(shù)(二)
瀏覽:29日期:2023-12-15 08:28:18
;PHP 和 XML: 使用expat函數(shù)(二)讓我們看一下實(shí)際處理這個(gè)文檔的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" => "nn", "STORY" => " ", "DATE" => "", "SLUG" => " ", "TEXT" => "", "PIC" => "", "NEWLINE" => "" ); $this->close_tag = array( "NEWSBOY" => " nnn", "STORY" => "", "DATE" => "", "SLUG" => " ", "TEXT" => "n", "PIC" => " " " ); } //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))); } } }} ?> -------------------------------------------------------------------------------- 在這個(gè)類的構(gòu)造函數(shù)中,我創(chuàng)建了打開與關(guān)閉兩個(gè)標(biāo)記數(shù)組。數(shù)組的關(guān)鍵字與我后面將要分析的標(biāo)記是 一樣的,并且它們相應(yīng)的值包含格式化打開與關(guān)閉標(biāo)記的HTML代碼。 我定義了一個(gè)簡(jiǎn)單的類析構(gòu)函數(shù)用來(lái)當(dāng)我們不再需要它時(shí)釋放XML 分析器。這個(gè)函數(shù)不得不手工調(diào)用, 因?yàn)镻HP不支持當(dāng)一個(gè)對(duì)象釋放時(shí)自動(dòng)調(diào)用類的析構(gòu)函數(shù)。 然后我定義了在XML文檔中用來(lái)分析打開和關(guān)閉標(biāo)記的主回調(diào)方法。我也定義了一個(gè)數(shù)據(jù)分析方法, 將 用于當(dāng)打開和關(guān)閉標(biāo)記中有數(shù)據(jù)時(shí),對(duì)數(shù)據(jù)進(jìn)行簡(jiǎn)單的格式化,后面我將向你演示如何將這些回調(diào)方法注冊(cè) 到分析器中。 在startElement和closeElement(當(dāng)分析到一個(gè)打開或關(guān)閉標(biāo)專時(shí)被分別調(diào)用)中使用 標(biāo)記的名字 作為索引鍵值對(duì)相應(yīng)的數(shù)組進(jìn)行查詢。如果那個(gè)鍵值存在,則返回值并且追加到類的'html' 屬性的后面。 'html'屬性將在以后我們真正顯示文檔內(nèi)容的時(shí)候使用。 characterData方法簡(jiǎn)單地將標(biāo)記之間的值加到類的html屬性的后面。 被注釋起來(lái)的叫PIHandler的方法是一個(gè)回調(diào)函數(shù),我還未曾實(shí)現(xiàn)它。如果它存在的話,它將直接在XML 文檔中處理php腳本。 現(xiàn)在,讓我解釋一下主要的分析方法的調(diào)用,你猜一猜,parse()!!! 第一行調(diào)用了函數(shù)xml_parser_create(),它將返回一個(gè)expat的xml分析器的實(shí)例,并且被保存在類的 屬性&this->xml_parser中。 下一步,我們需要用函數(shù)xml_set_object()來(lái)注冊(cè)一個(gè)類方法的回調(diào)函數(shù)。 我是這樣使用的,xml_set_object($this->xml_parser, &$this)。我在第一個(gè)參數(shù)中指定了用 來(lái)保存xml 分析器的類屬性,然后在第二個(gè)參數(shù),我指定了PHP對(duì)象的實(shí)例地址。這個(gè)可以讓分析器 知道全 部將要注冊(cè)的回調(diào)函數(shù),是在那個(gè)地址上指定類的實(shí)際的方法。這就象c或c++中的一個(gè)'引用傳遞',也有人 簡(jiǎn)單地叫做'引用變量'。 在下一行,我調(diào)用了xml_parser_set_option()設(shè)置了一個(gè)xml分析器的屬性,使用大小寫折疊( case folding)。大小寫折疊只是告訴分析器知道,當(dāng)我分析我的XML文檔時(shí)我并不關(guān)心大小寫敏感,但是 如果你 想使用大小寫敏感來(lái)定義兩個(gè)不同的標(biāo)記,如或,你可以不設(shè)置它。 通過(guò)使用xml_set_element_handler(),我指定了用于開始和結(jié)束標(biāo)記的回調(diào)函數(shù),名字是 "startElement"和"endElement"。 接著,我使用xml_set_character_data_handler()來(lái)指定字符數(shù)據(jù)的處理句柄為名為 characterData()的回調(diào)函數(shù)。被注釋的函數(shù)調(diào)用,xml_set_processing_instruction_handler(), 是一個(gè)我用于注冊(cè)函數(shù) PIHandler()的調(diào)用。PIHandler可以被包括在XML文檔中處理php代碼。 其它的代碼只是很簡(jiǎn)單地讀XML文件并且分析它。如果一個(gè)錯(cuò)誤發(fā)生,那么錯(cuò)誤明細(xì)將返回,包括錯(cuò)誤 發(fā)生的行號(hào)。;;
標(biāo)簽:
PHP
排行榜
