国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:17日期:2023-12-15 08:28:18
;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: 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))); } } }} ?> -------------------------------------------------------------------------------- 在這個類的構造函數中,我創建了打開與關閉兩個標記數組。數組的關鍵字與我后面將要分析的標記是 一樣的,并且它們相應的值包含格式化打開與關閉標記的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文件并且分析它。如果一個錯誤發生,那么錯誤明細將返回,包括錯誤 發生的行號。;;
標簽: PHP
主站蜘蛛池模板: 国产精品 色| 国产三级日本三级在线播放 | 在线视频 中文字幕 | 美女视频黄视大全视频免费网址 | 中文字幕在线观看不卡视频 | 欧美在线观看一区二区 | 在线看片 在线播放 | 欧美高h视频 | 美女操穴视频 | 欧美三级黄色 | 亚洲精品在线免费看 | 日韩高清欧美 | 国产成人精品视频播放 | 女人被男人躁得好爽免费文 | 欧美的高清视频在线观看 | 小泽玛利亚的一级毛片的 | 国产日韩欧美在线观看不卡 | 国产成人精品一区二三区 | 国产偷怕自拍 | 国产做国产爱免费视频 | 国产综合13p| 一本久道久久综合婷婷五 | 自拍第一页 | 国产91网址 | 美女张开大腿让男人捅 | 超清国产粉嫩456在线免播放 | 3d动漫精品成人一区二区三 | 高清在线一区二区三区亚洲综合 | 99视频免费观看 | 亚洲欧美日产综合在线看 | 精品午夜国产在线观看不卡 | 成人免费观看永久24小时 | 国产精品黄在线观看免费 | 亚洲国产日韩欧美综合久久 | 久久精品成人欧美大片免费 | 久久九九有精品国产56 | 国产一级一片免费播放i | 国产午夜精品理论片在线 | 欧美午夜免费一级毛片 | 欧美一区二区三区不卡免费观看 | 久久成人福利视频 |