分享
 
 
 

xml解析类

王朝other·作者佚名  2006-12-16
窄屏简体版  字體: |||超大  

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();

?>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有