讓我們看一下實際處理這個文檔的PHP代碼。



/*NewsBoy : News system for the web written in PHP by Justin Grant (Web: jusgrant.cjb.net or justin.host.za.net Mail: jus " /> 国产色91在线,heyzo一区,日本一区二区不卡

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

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

 php 和 XML: 使用expat函數(shù)(二)
  讓我們看一下實際處理這個文檔的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))); } } }}
?>



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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

php技術PHP 和 XML: 使用expat函數(shù)(二),轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 林甸县| 横山县| 肃宁县| 鄯善县| 黔江区| 乌海市| 雅安市| 玉屏| 库车县| 土默特右旗| 宁化县| 景宁| 绍兴县| 新巴尔虎左旗| 宁化县| 乌鲁木齐县| 镇原县| 巧家县| 定南县| 通道| 凤台县| 花垣县| 崇州市| 赣榆县| 丹凤县| 台中县| 肥东县| 兰州市| 崇礼县| 潞城市| 仁寿县| 名山县| 大关县| 东山县| 科技| 札达县| 泰顺县| 灵台县| 兴业县| 哈巴河县| 镇沅|