xml 解析类
ob_start();
################################
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 解析器.”;
return false;
}
}#Function 分析XML End.
function SetNeedParseBlock($NeedParseBlock){
$this->NeedParseBlockArray=$NeedParseBlock;
}
function SetXMLData($XMLData=NULL){//设置XML数据
if($XMLData==””){ if($this->debug) echo “XML 数据为空.”;exit();
return false;
}
$this->XMLData=$XMLData;
}#Function SetXMLData End.
function GetXMLData(){
if($this->XMLData==””){ if($this->debug) echo “还没有设置 XML 数据.”;exit();
return false;
}
return $this->XMLData;
}#Function getXMLData End.
function ParseXMLData(){
if($this->debug && !$this->ParseHandle) echo “不能创建 XML 解析器.”;
//if(!is_array($this->NeedParseArray) && $this->debug) echo “请设置要分析的标记.”;
$XMLData=$this->GetXMLData();
$this->XMLData=preg_replace(‘/<\?xml(.*)?\?’.’>/’, ”, $XMLData);
if( trim($this->XMLData)==”” ){ if($this->debug) echo “XML 数据错误.”;
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.
#===========================
# 使用实例
#
#===========================
ob_end_flush();
?>