PHP解析xml格式数据工具类示例
本文实例讲述了PHP解析xml格式数据工具类。分享给大家供大家参考,具体如下:
classome_xml{
/**
*xml资源
*
*@varresource
*@seexml_parser_create()
*/
public$parser;
/**
*资源编码
*
*@varstring
*/
public$srcenc;
/**
*targetencoding
*
*@varstring
*/
public$dstenc;
/**
*theoriginalstruct
*
*@accessprivate
*@vararray
*/
public$_struct=array();
/**
*Constructor
*
*@accesspublic
*@parammixed[$srcenc]sourceencoding
*@parammixed[$dstenc]targetencoding
*@returnvoid
*@since
*/
functionSofeeXmlParser($srcenc=null,$dstenc=null){
$this->srcenc=$srcenc;
$this->dstenc=$dstenc;
//initializethevariable.
$this->parser=null;
$this->_struct=array();
}
/**
*ParsestheXMLfile
*
*@accesspublic
*@paramstring[$file]theXMLfilename
*@returnvoid
*@since
*/
functionxml2array($file){
//$this->SofeeXmlParser('utf-8');
$data=file_get_contents($file);
$this->parseString($data);
return$this->getTree();
}
functionxml3array($file){
$data=file_get_contents($file);
$this->parseString($data);
return$this->_struct;
}
/**
*Parsesastring.
*
*@accesspublic
*@paramstringdataXMLdata
*@returnvoid
*/
functionparseString($data){
if($this->srcenc===null){
$this->parser=xml_parser_create();
}else{
if($this->parser=xml_parser_create($this->srcenc)){
return'UnabletocreateXMLparserresourcewith'.$this->srcenc.'encoding.';
}
}
if($this->dstenc!==null){
@xml_parser_set_option($this->parser,XML_OPTION_TARGET_ENCODING,$this->dstenc)ordie('Invalidtargetencoding');
}
xml_parser_set_option($this->parser,XML_OPTION_CASE_FOLDING,0);//lowercasetags
xml_parser_set_option($this->parser,XML_OPTION_SKIP_WHITE,1);//skipemptytags
if(!xml_parse_into_struct($this->parser,$data,$this->_struct)){
/*printf("XMLerror:%satline%d",
xml_error_string(xml_get_error_code($this->parser)),
xml_get_current_line_number($this->parser)
);*/
$this->free();
returnfalse;
}
$this->_count=count($this->_struct);
$this->free();
}
/**
*returnthedatastruction
*
*@accesspublic
*@returnarray
*/
functiongetTree(){
$i=0;
$tree=array();
$tree=$this->addNode(
$tree,
$this->_struct[$i]['tag'],
(isset($this->_struct[$i]['value']))?$this->_struct[$i]['value']:'',
(isset($this->_struct[$i]['attributes']))?$this->_struct[$i]['attributes']:'',
$this->getChild($i)
);
unset($this->_struct);
return$tree;
}
/**
*recursionthechildrennodedata
*
*@accesspublic
*@paraminteger[$i]thelaststructindex
*@returnarray
*/
functiongetChild(&$i){
//containnodedata
$children=array();
//loop
while(++$i<$this->_count){
//nodetagname
$tagname=$this->_struct[$i]['tag'];
$value=isset($this->_struct[$i]['value'])?$this->_struct[$i]['value']:'';
$attributes=isset($this->_struct[$i]['attributes'])?$this->_struct[$i]['attributes']:'';
switch($this->_struct[$i]['type']){
case'open':
//nodehasmorechildren
$child=$this->getChild($i);
//appendthechildrendatatothecurrentnode
$children=$this->addNode($children,$tagname,$value,$attributes,$child);
break;
case'complete':
//atendofcurrentbranch
$children=$this->addNode($children,$tagname,$value,$attributes);
break;
case'cdata':
//nodehasCDATAafteroneofit'schildren
$children['value'].=$value;
break;
case'close':
//endofnode,returncollecteddata
return$children;
break;
}
}
//return$children;
}
/**
*Appendssomevaluestoanarray
*
*@accesspublic
*@paramarray[$target]
*@paramstring[$key]
*@paramstring[$value]
*@paramarray[$attributes]
*@paramarray[$inner]thechildren
*@returnvoid
*@since
*/
functionaddNode($target,$key,$value='',$attributes='',$child=''){
if(!isset($target[$key]['value'])&&!isset($target[$key][0])){
if($child!=''){
$target[$key]=$child;
}
if($attributes!=''){
foreach($attributesas$k=>$v){
$target[$key][$k]=$v;
}
}
$target[$key]['value']=$value;
}else{
if(!isset($target[$key][0])){
//isstringorother
$oldvalue=$target[$key];
$target[$key]=array();
$target[$key][0]=$oldvalue;
$index=1;
}else{
//isarray
$index=count($target[$key]);
}
if($child!=''){
$target[$key][$index]=$child;
}
if($attributes!=''){
foreach($attributesas$k=>$v){
$target[$key][$index][$k]=$v;
}
}
$target[$key][$index]['value']=$value;
}
return$target;
}
/**
*Freetheresources
*
*@accesspublic
*@returnvoid
**/
functionfree(){
if(isset($this->parser)&&is_resource($this->parser)){
xml_parser_free($this->parser);
unset($this->parser);
}
}
}
PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:
在线