xml 解析類
更新時(shí)間:2006年10月11日 00:00:00 作者:
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 分析類.上面是實(shí)用的例子,返回結(jié)果是一個(gè)二維數(shù)組 格式 $Result[x][xml標(biāo)記](méi)=xml標(biāo)記值;
這里沒(méi)有從 http 中取數(shù)據(jù),要用另外一個(gè)類取數(shù)據(jù).
*/
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 "不能創(chuàng)建 XML 解析器.<br />";
return false;
}
}#Function 分析XML End.
/*
function SetNeedParse($NeedParse){
$this->NeedParseArray=$NeedParse;
}*/
function SetNeedParseBlock($NeedParseBlock){
$this->NeedParseBlockArray=$NeedParseBlock;
}
function SetXMLData($XMLData=NULL){//設(shè)置XML數(shù)據(jù)
if($XMLData==""){ if($this->debug) echo "XML 數(shù)據(jù)為空.<br />";exit();
return false;
}
$this->XMLData=$XMLData;
}#Function SetXMLData End.
function GetXMLData(){
if($this->XMLData==""){ if($this->debug) echo "還沒(méi)有設(shè)置 XML 數(shù)據(jù).<br />";exit();
return false;
}
return $this->XMLData;
}#Function getXMLData End.
function ParseXMLData(){
if($this->debug && !$this->ParseHandle) echo "不能創(chuàng)建 XML 解析器.<br />";
//if(!is_array($this->NeedParseArray) && $this->debug) echo "請(qǐng)?jiān)O(shè)置要分析的標(biāo)記.<br />";
$XMLData = $this->GetXMLData();
$this->XMLData = preg_replace('/<\?xml(.*)?\?'.'>/', '', $XMLData);
if( trim($this->XMLData)=="" ){ if($this->debug) echo "XML 數(shù)據(jù)錯(cuò)誤.<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數(shù)據(jù) End.
function StartTag($ParseHandle,$Tag,$Attrib){#解析器句柄,標(biāo)記名稱,標(biāo)記屬性(數(shù)組)
$this->OpenTag[$Tag];
}#Function 起始標(biāo)記 End.
function CData($ParseHandle=NULL,$DataString=NULL){
$this->TmpData=$DataString;
}#Function 取數(shù)據(jù) 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 結(jié)束標(biāo)記 End.
function Result(){
$aTmp=$this->ReturnData;
return $aTmp;
}
#----------------------
}#Class 分析XML End.
#===========================
# 使用實(shí)例
#
#===========================
/*
$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);//設(shè)置需要分析的標(biāo)記
$xx->SetNeedParseBlock($NeedParseBlock);//設(shè)置需要分析的標(biāo)記塊 比如上面的塊是 ValidError
$xx->SetXMLData($XMLData);
$xx->GetXMLData();
$xx->ParseXMLData(); //如果 === false 說(shuō)明分析數(shù)據(jù)時(shí)發(fā)生錯(cuò)誤
$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();
?>
相關(guān)文章
XML卷之實(shí)戰(zhàn)錦囊(5):結(jié)構(gòu)樹(shù)圖
XML卷之實(shí)戰(zhàn)錦囊(5):結(jié)構(gòu)樹(shù)圖...2006-10-10用ASP+Access創(chuàng)建網(wǎng)站RSS格式內(nèi)容摘要
用ASP+Access創(chuàng)建網(wǎng)站RSS格式內(nèi)容摘要...2006-08-08XML輕松學(xué)習(xí)手冊(cè)(五):XML語(yǔ)法
XML輕松學(xué)習(xí)手冊(cè)(五):XML語(yǔ)法...2006-10-10XML加ASP實(shí)現(xiàn)網(wǎng)頁(yè)“本地化”
XML加ASP實(shí)現(xiàn)網(wǎng)頁(yè)“本地化”...2006-08-08使用XSLT將XML數(shù)據(jù)轉(zhuǎn)換成HTML
使用XSLT將XML數(shù)據(jù)轉(zhuǎn)換成HTML...2006-10-10使用正則表達(dá)式進(jìn)行xml數(shù)據(jù)驗(yàn)證
使用正則表達(dá)式進(jìn)行xml數(shù)據(jù)驗(yàn)證...2006-10-10