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; /