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 解析器.
";
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 数据为空.
";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.
#===========================
# 使用实例
#
#===========================
/*
$XMLData= <<< EOD
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]."
";
echo $aa[0][ErrMSG]."
";
echo $aa[0][ValidErrorInfo]."
";
echo "
";
echo $aa[1][ValidErrorCode]."
";
echo $aa[1][ErrMSG]."
";
echo $aa[1][ValidErrorInfo]."
";
for($i=0;$i<=count($aa)-1;$i++){
//echo $aa[$i][ValidErrorCode];
//echo $aa[$i][ValidErrorInfo]."
";
}
#===========================
*/
ob_end_flush();
?>
相关视频
相关阅读 Windows错误代码大全 Windows错误代码查询激活windows有什么用Mac QQ和Windows QQ聊天记录怎么合并 Mac QQ和Windows QQ聊天记录Windows 10自动更新怎么关闭 如何关闭Windows 10自动更新windows 10 rs4快速预览版17017下载错误问题Win10秋季创意者更新16291更新了什么 win10 16291更新内容windows10秋季创意者更新时间 windows10秋季创意者更新内容kb3150513补丁更新了什么 Windows 10补丁kb3150513是什么
热门文章 360快剪辑怎么使用 36金山词霸如何屏幕取词百度收购PPS已敲定!3
最新文章
微信3.6.0测试版更新了微信支付漏洞会造成哪
360快剪辑怎么使用 360快剪辑软件使用方法介酷骑单车是什么 酷骑单车有什么用Apple pay与支付宝有什么区别 Apple pay与贝贝特卖是正品吗 贝贝特卖网可靠吗
人气排行 xp系统停止服务怎么办?xp系统升级win7系统方电脑闹钟怎么设置 win7电脑闹钟怎么设置office2013安装教程图解:手把手教你安装与qq影音闪退怎么办 QQ影音闪退解决方法VeryCD镜像网站逐个数,电驴资料库全集同步推是什么?同步推使用方法介绍QQ2012什么时候出 最新版下载EDiary——一款好用的电子日记本
查看所有0条评论>>