欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

xml 解析类

程序员文章站 2022-03-22 08:18:47
xml 解析类

xml 解析类 <?php
ob_start();
/*                                                              
* copyright (c) 2003 nightkids  <weidewang@magus-soft.com>                     
* all rights reserved.                                                        
*
* redistribution and use in source and binary forms, with or without         
* modification, are permitted provided that the following conditions         
* are met:                                                                    
* 1. redistributions of source code must retain the above copyright         
*    notice, this list of conditions and the following disclaimer.            
* 2. redistributions in binary form must reproduce the above copyright        
*    notice, this list of conditions and the following disclaimer in the      
*    documentation and/or other materials provided with the distribution.   
*
* this software is provided by the author and contributors ``as is'' and      
* any express or implied warranties, including, but not limited to, the      
* implied warranties of merchantability and fitness for a particular purpose
* are disclaimed.  in no event shall the author or contributors be liable   
* for any direct, indirect, incidental, special, exemplary, or consequential
* damages (including, but not limited to, procurement of substitute goods   
* or services; loss of use, data, or profits; or business interruption)      
* however caused and on any theory of liability, whether in contract, strict
* liability, or tort (including negligence or otherwise) arising in any way  
* out of the use of this software, even if advised of the possibility of      
* such damage.
  */                    
################################
/*
last modify 2003-10-24  10:41:11
xml 分析类.上面是实用的例子,返回结果是一个二维数组 格式 $result[x][xml标记]=xml标记值;
这里没有从 http 中取数据,要用另外一个类取数据.
*/
class z99parsexml{
#----------------------
   var $parsehandle=null; //解析器指针
   var $charset="utf-8";//编码方式
   var $tag=null;
   var $attrib=array();
   var $datastring=null;
   var $xmldata=null;

   var $debug = false;

   var $returndata=array();

   var $currenttag=null;
   var $opentag=array();
   var $closetag=array();

   //var $needparsearray=array();
   var $needparseblockarray=array();
   var $tagsum=0;

   var $currenttaga=null;

   var $tmpdata=null;
#----------------------
   function z99parsexml(){
      $this->parsehandle = xml_parser_create($this->charset);      
      if(!$this->parsehandle){ echo "不能创建 xml 解析器.<br />";
         return false;
      }
   }#function 分析xml end.
   /*
   function setneedparse($needparse){
      $this->needparsearray=$needparse;
   }*/
   function setneedparseblock($needparseblock){
      $this->needparseblockarray=$needparseblock;
   }
   function setxmldata($xmldata=null){//设置xml数据
      if($xmldata==""){ if($this->debug) echo "xml 数据为空.<br />";exit();
         return false;
      }
      $this->xmldata=$xmldata;
   }#function setxmldata end.

   function getxmldata(){
      if($this->xmldata==""){   if($this->debug) echo "还没有设置 xml 数据.<br />";exit();
         return false;
      }
      return $this->xmldata;
   }#function getxmldata end.   

   function parsexmldata(){   
      if($this->debug && !$this->parsehandle) echo "不能创建 xml 解析器.<br />";   
      //if(!is_array($this->needparsearray) && $this->debug) echo "请设置要分析的标记.<br />";
        $xmldata = $this->getxmldata();
        $this->xmldata = preg_replace('/<\?xml(.*)?\?'.'>/', '', $xmldata);
        if( trim($this->xmldata)=="" ){ if($this->debug) echo "xml 数据错误.<br />";
           return false;
          }
          xml_parser_set_option($this->parsehandle, xml_option_case_folding, false);
      xml_parser_set_option($this->parsehandle, xml_option_target_encoding, $this->charset);
      xml_set_object($this->parsehandle, $this);
      xml_set_element_handler($this->parsehandle, 'starttag', 'endtag');
      xml_set_character_data_handler($this->parsehandle, 'cdata');
      if (!xml_parse($this->parsehandle, $this->xmldata)) {
         if($this->debug)
             die(sprintf('xml error: %s at line %d',
                xml_error_string(xml_get_error_code($this->parsehandle)),
                xml_get_current_line_number($this->parsehandle)));
            return false;
        }
   }#function 解析xml数据 end.

   function starttag($parsehandle,$tag,$attrib){#解析器句柄,标记名称,标记属性(数组)      
      $this->opentag[$tag];      
   }#function 起始标记 end.

   function cdata($parsehandle=null,$datastring=null){   
      $this->tmpdata=$datastring;
   }#function 取数据 end.

   function endtag($parsehandle,$tag){
      $needparse=$this->needparseblockarray;      
      $this->closetag[$tag];
      $this->currenttag=$tag;
      $tmpdata=$this->tmpdata;
      $datalen=strlen(trim($tmpdata));
      if( $datalen !=0 ){
         $this->returndata[$this->tagsum][$tag]=$tmpdata;
      }
      if(array_search($tag,$needparse)!==false) $this->tagsum++;      
   }#function 结束标记 end.      
   function result(){
      $atmp=$this->returndata;
      return $atmp;
   }
#----------------------
}#class 分析xml end.
#===========================
# 使用实例
#
#===========================
/*
$xmldata= <<< eod
<u-max>
    <validerror>
        <validerrorcode>errorcode</validerrorcode>
        <validerrorinfo>the 'productcode' element has an invalid value according to its data type. an error occurred at(35, 29).</validerrorinfo>
      <errmsg>eraer23 asdf sgs</errmsg>
    </validerror>
   <validerror>
        <validerrorcode>errorcode1</validerrorcode>
        <validerrorinfo>111the 'productcode' element has an invalid value according to its data type. an error occurred at(35, 29).</validerrorinfo>
      <errmsg>errf aeaa sgs</errmsg>
    </validerror>
   <validerror>
        <validerrorcode>errorco234</validerrorcode>
        <validerrorinfo>111the 'productcode' element has an invalid value according to its data type. an error occurred at(35, 29).</validerrorinfo>
      <errmsg>errfae asdfsgs</errmsg>
    </validerror>
   <validerror>
        <validerrorcode>erasdfasode1</validerrorcode>
        <validerrorinfo>11fasd asdas a sd29).</validerrorinfo>
      <errmsg>eafe3aeasgs</errmsg>
    </validerror>
</u-max>
eod;
#===========================   
   //$needparse=array("validerrorcode","validerrorinfo");
   $needparseblock=array("validerror","fuck");
   $xx = new z99parsexml();   
   $xx->debug=true;   
   //$xx->setneedparse($needparse);//设置需要分析的标记
   $xx->setneedparseblock($needparseblock);//设置需要分析的标记块 比如上面的块是 validerror
   $xx->setxmldata($xmldata);
   $xx->getxmldata();   
   $xx->parsexmldata();   //如果 === false 说明分析数据时发生错误

   $aa=$xx->result();
   echo $aa[0][validerrorcode]."<br>";
   echo $aa[0][errmsg]."<br>";
   echo $aa[0][validerrorinfo]."<br>";
   echo "<br>";
   echo $aa[1][validerrorcode]."<br>";
   echo $aa[1][errmsg]."<br>";
   echo $aa[1][validerrorinfo]."<br>";
   for($i=0;$i<=count($aa)-1;$i++){
      //echo $aa[$i][validerrorcode];
      //echo $aa[$i][validerrorinfo]."<br>";
   }   
#===========================
*/
ob_end_flush();
?>