|
网络发烧友

|
1#
大 中
小 发表于 2007-2-13 10:26 只看该作者
php 和 xml: 使用expat函数(二)
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; /
|