ParseOut_class.php
上传用户:yttaitong
上传日期:2009-05-10
资源大小:128k
文件大小:3k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

PHP

  1. <?PHP
  2. /*
  3.     [BBWPS!] (C)2006-2010 小蜜蜂版权所有.
  4. This is NOT a freeware, use is subject to license terms
  5.     时间:2007年12月
  6.     描述:
  7. */
  8. class OutParse{
  9. var $url;       
  10. var $data;       
  11. var $items;     
  12. var $xml_parser; 
  13. var $depth="2"; 
  14. var $tag="OUT";  
  15. var $prev_tag;   
  16. var $marker="2"; 
  17. var $event;     
  18. var $item_index; 
  19. function __construct($Out_url=""){
  20. $this->OutParse($Out_url);
  21. }
  22. function OutParse($Out_url=NULL){
  23. if(get_cfg_var("allow_url_fopen")){
  24. $fp=fopen($Out_url,"r");
  25. if($fp){
  26.   while(!feof($fp)){$this->data.=fgets($fp,4096);}
  27.   }
  28. fclose($fp);
  29. }
  30. else{$this->data=openFileRemote($Out_url);}
  31. if($this->data){
  32. if(function_exists("xml_parser_create")){
  33. if(!$this->ParseXML()){
  34. return null;
  35. }
  36. }
  37. else{
  38. $this->ParseReg();
  39. }
  40. }
  41. else{
  42. return null;
  43. }
  44. }
  45. function GetItems(){
  46. return $this->items;
  47. }
  48. function GetVersion(){
  49. return $this->version;
  50. }
  51. function startElement($parser, $name, $attribs){
  52. $this->depth++;
  53. $this->tag=$name;
  54. switch($name){
  55. case "OUT":
  56. $this->event=$name;
  57. $this->version=$attribs["VERSION"];
  58. break;
  59. case "ITEM":
  60. $this->item_index++;
  61. $this->event=$name;
  62. $this->marker=$this->depth+1;
  63. break;
  64. default:
  65. return NULL;
  66. }
  67. }
  68. function endElement($parser, $name){
  69. $this->depth--;
  70. return null;
  71. }
  72. function characterData($parser, $data){
  73. if( $this->event=="ITEM" && $this->marker==$this->depth ){
  74. if($this->prev_tag==$this->tag){
  75. $this->items[$this->item_index][$this->tag].=rawurldecode($data);
  76. }
  77. else{
  78. $this->items[$this->item_index][$this->tag]=rawurldecode($data);
  79. }
  80. }
  81. $this->prev_tag=$this->tag;
  82. }
  83. function ParseXML(){
  84. $this->xml_parser = xml_parser_create();
  85. xml_set_object($this->xml_parser, &$this);
  86. xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, 1);
  87. xml_set_element_handler($this->xml_parser, "startElement", "endElement");
  88. xml_set_character_data_handler($this->xml_parser, "characterData");
  89. if (!xml_parse($this->xml_parser, $this->data)){
  90. return false;
  91. }
  92. return true;
  93. }
  94. function ParseReg(){
  95. preg_match_all("/<item>(.*?)</item>/is",$this->data,$itemArray);
  96. foreach ($itemArray[1] as $item){
  97. $this->item_index++;
  98. preg_match_all("/<([a-zA-Z]+)>(.*?)</\1>/is",$item,$tagArray);
  99. foreach ($tagArray[2] as $key=>$data){
  100. $this->items[$this->item_index][strtoupper(trim($tagArray[1][$key]))]=rawurldecode($data);
  101. }
  102. }
  103. }
  104. }
  105. ?>