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

来源:互联网 发布:java 图片合成工具 编辑:程序博客网 时间:2024/05/21 19:40
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
PHPXML: 使用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; /<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>