读取与生成XML

 写这个文章主要是受数据库操作的影响,数据库有读取数据库,查询数据库等等的操作,我们总是会去把他写成一个数据库的类方便工作中使用,如果某一个项目的数据库并没有用数据库来集成而是用XML的,如果也有一个这样的类不是也很方便

<?php

/* Author:Yustar @ 2007-01-06
 * xml操作类,读取、分析xml
 * 本类在xmlize基础上改进封装
 
*/
class xmlparsing

{
  
  
var $data;

  
function xmlparsing($data) {

    
$this->data = $file;

  }
  
  
function check($data){

    
if(file_exists($data)){
        
return 1;
    }
else{
        
return 0;
    }

  
function xmlize($data, $WHITE=1) {

    
$data = trim($data);
    
$vals = $index = $array = array();
    
$parser = xml_parser_create();
    
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $WHITE);
    
if ( !xml_parse_into_struct($parser, $data, $vals, $index) )
    {
    
die(sprintf("XML error: %s at line %d",
                    
xml_error_string(xml_get_error_code($parser)),
                    
xml_get_current_line_number($parser)));

    }
    
xml_parser_free($parser);

    
$i = 0

    
$tagname = $vals[$i]['tag'];
    
if ( isset ($vals[$i]['attributes'] ) )
    {
        
$array[$tagname]['@'= $vals[$i]['attributes'];
    } 
else {
        
$array[$tagname]['@'= array();
    }

    
$array[$tagname]["#"= xml_depth($vals, $i);

    
return $array;
  }

/* 
 *
 * You don't need to do anything with this function, it's called by
 * xmlize.  It's a recursive function, calling itself as it goes deeper
 * into the xml levels.  If you make any improvements, please let me know.
 *
 *
 
*/

 
function xml_depth($vals, &$i) { 
    
$children = array(); 

    
if ( isset($vals[$i]['value']) )
    {
        
array_push($children, $vals[$i]['value']);
    }

    
while (++$i < count($vals)) { 

        
switch ($vals[$i]['type']) { 

           
case 'open': 

                
if ( isset ( $vals[$i]['tag'] ) )
                {
                    
$tagname = $vals[$i]['tag'];
                } 
else {
                    
$tagname = '';
                }

                
if ( isset ( $children[$tagname] ) )
                {
                    
$size = sizeof($children[$tagname]);
                } 
else {
                    
$size = 0;
                }

                
if ( isset ( $vals[$i]['attributes'] ) ) {
                    
$children[$tagname][$size]['@'= $vals[$i]["attributes"];
                }

                
$children[$tagname][$size]['#'= xml_depth($vals, $i);

            
break


            
case 'cdata':
                
array_push($children, $vals[$i]['value']); 
            
break

            
case 'complete': 
                
$tagname = $vals[$i]['tag'];

                
ifisset ($children[$tagname]) )
                {
                    
$size = sizeof($children[$tagname]);
                } 
else {
                    
$size = 0;
                }

                
ifisset ( $vals[$i]['value'] ) )
                {
                    
$children[$tagname][$size]["#"= $vals[$i]['value'];
                } 
else {
                    
$children[$tagname][$size]["#"= '';
                }

                
if ( isset ($vals[$i]['attributes']) ) {
                    
$children[$tagname][$size]['@']
                                             
= $vals[$i]['attributes'];
                }            

            
break

            
case 'close':
                
return $children
            
break;
        } 

    } 

    
return $children;

 }


/* function by acebone@f2s.com, a HUGE help!
 *
 * this helps you understand the structure of the array xmlize() outputs
 *
 * usage:
 * traverse_xmlize($xml, 'xml_');
 * print '<pre>' . implode("", $traverse_array . '</pre>';
 *
 *
 
*/ 

 
function traverse_xmlize($array, $arrName = "array", $level = 0) {

    
foreach($array as $key=>$val)
    {
        
if ( is_array($val) )
        {
            traverse_xmlize(
$val, $arrName . "[" . $key . "]", $level + 1);
        } 
else {
            
$GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "" ";
        }
    }

    
return 1;

 }
}
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值