程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> PHP 和 XML: 使用expat函數(二)

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

編輯:PHP綜合
 PHP 和 XML: 使用expat函數(二)
  讓我們看一下實際處理這個文檔的PHP代碼。



/*NewsBoy : News system for the web written in PHP by Justin Grant (Web: jusgrant.cjb.net or justin.host.za.net Mail: [email protected])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))); } } }}
?>



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

  在這個類的構造函數中,我創建了打開與關閉兩個標記數組。數組的關鍵字與我後面將要分析的標記是
一樣的,並且它們相應的值包含格式化打開與關閉標記的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文檔時我並不關心大小寫敏感,但是 如果你
想使用大小寫敏感來定義兩個不同的標記,如或,你可以不設置它。

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

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

  其它的代碼只是很簡單地讀XML文件並且分析它。如果一個錯誤發生,那麼錯誤明細將返回,包括錯誤
發生的行號。  
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved