Ver Mensaje Individual
  #2 (permalink)  
Antiguo 06/05/2005, 11:25
Avatar de sism82
sism82
 
Fecha de Ingreso: octubre-2003
Ubicación: Guadalajara
Mensajes: 865
Antigüedad: 20 años, 6 meses
Puntos: 1
pues no se donde hayas buscado, pero la red esta repleta de esa clase de información, para prueba basta consultar la documentación de php para la función xml_parse_into_struct y similares.

Adicionalmente te dejo esta clase, unicamente la escribí y probe ligeramente, puede contener algún error de lógica bajo condiciones extrañas. Pero creo que está entendible.

Código PHP:
<?php
/********************* Xml_Config  ******************/
/*
this class uses the php xml parser to construct a multidimensional array with the values of an XML file
*/
include_once('dbLink.php');
class 
Xml_Config {
    
    protected 
$db      NULL;
    public 
$xml_parser NULL;
    public 
$xml_values = array();
    public 
$xml_indexs = array();
    public 
$xml_struct = array();
    public 
$xml_data   "";
    public 
$xml_file   "";
    
    public function 
__construct($file "") {
        
$this->xml_parser           xml_parser_create();
        
$this->db                   = new dbLink();
        if ( 
$file != "" ) {
            
$this->setXmlFile($file);
            return 
$this->readXml();
        } else {
            return 
true;
        }
    }
    
    public function 
setXmlFile($file) {
        
$this->xml_file $file;
        return 
true;
    }
    
    public function 
readXml() {
        if ( 
is_file($this->xml_file) && is_readable($this->xml_file) ) {
            
$data file_get_contents($this->xml_file);
            
xml_parse_into_struct($this->xml_parser,$data,$this->xml_values,$this->xml_indexs);
            
$this->xml_values $this->xmlRemoveCdata($this->xml_values);
            
$xml_level_tags $this->getXmlLevelTags(1);
            
$this->createXmlStruct($xml_level_tags,$this->xml_struct);
            return 
true;
        } else {
            return 
false;
        }
    }
    
    public function 
readXmlFromDB() {
        
$sql "SELECT file_content FROM config_files WHERE file_name = '".$this->xml_file."'";
        
$this->db->sqlQuery($sql);
        
$rss $this->db->getRssData();
        if ( 
is_object($rss) ) {
            
$this->xml_struct = @unserialize($rss->file_content) ? unserialize($rss->file_content) : array();
        } else {
            return 
false;
        }
    }
    
    
//i couldnt find a good reason to preserve the cdata, so, i delete it :)
    
protected function xmlRemoveCdata($struct) {
        
$new_struct = array();
        foreach ( 
$struct as $index => $props ) {
            if ( 
$props['type'] != "cdata" ) {
                
$new_struct[] = $props;
            }
        }
        return 
$new_struct;
    }
    
    
//create the multidimensional array depending on the values returned by the php xml parser (recursive function)
    
protected function createXmlStruct($xml_level_tags,&$xml_struct) {
        foreach ( 
$xml_level_tags as $index => $keys ) {
            
$global_ix $keys['global_index'];
            
$props     $keys['values'];
            
$tag_type  $props['type'];
            
$tag       $props['tag'];
            
$tag_value = isset($props['value'])      ? $props['value']      : "";
            
$tag_atts  = isset($props['attributes']) ? $props['attributes'] : array();
            
$tag_level $props['level'];
            switch ( 
$tag_type ) {
                
                case 
'cdata':
                    continue;
                    break;
                    
                case 
'open':
                    if ( !
array_key_exists($tag,$xml_struct) ) {
                        
$xml_struct[$tag] = array();
                    }
                    
$next count($xml_struct[$tag]);
                    
$xml_struct[$tag][$next]               = array();
                    
$xml_struct[$tag][$next]['sub-tags']   = array();
                    
$xml_struct[$tag][$next]['attributes'] = array();
                    
$xml_struct[$tag][$next]['value']      = $tag_value;
                    
$xml_struct[$tag][$next]['type']       = 'open';
                    
$xml_struct[$tag][$next]['level']      = $tag_level;
                    foreach ( 
$tag_atts as $attribute => $att_value ) {
                        if ( !
array_key_exists($attribute,$xml_struct[$tag][$next]['attributes']) ) {
                            
$xml_struct[$tag][$next]['attributes'][$attribute] = array();
                        }
                        
$xml_struct[$tag][$next]['attributes'][$attribute][] = $att_value;
                    }
                    
$tag_children = array();
                    
$tag_children $this->getTagChildren($global_ix);
                    
$this->createXmlStruct($tag_children,$xml_struct[$tag][$next]['sub-tags']);
                    break;
                    
                case 
'close':
                    continue;
                    break;
                
                case 
'complete':
                    if ( !
array_key_exists($tag,$xml_struct) ) {
                        
$xml_struct[$tag] = array();
                    }
                    
$next count($xml_struct[$tag]);
                    
$xml_struct[$tag][$next]               = array();
                    
$xml_struct[$tag][$next]['sub-tags']   = array();
                    
$xml_struct[$tag][$next]['attributes'] = array();
                    
$xml_struct[$tag][$next]['value']      = $tag_value;
                    
$xml_struct[$tag][$next]['type']       = 'complete';
                    
$xml_struct[$tag][$next]['level']      = $tag_level;
                    foreach ( 
$tag_atts as $attribute => $att_value ) {
                        if ( !
array_key_exists($attribute,$xml_struct[$tag][$next]['attributes']) ) {
                            
$xml_struct[$tag][$next]['attributes'][$attribute] = array();
                        }
                        
$xml_struct[$tag][$next]['attributes'][$attribute][] = $att_value;
                    }
                    break;
                
                default:
                    break;
            }
        }
    }
    
    
//creates xml data from an xml struct
    
public function createXml(&$xmldata NULL,$tags NULL,&$pending_tags = array(),$default_level 1) {
        if ( 
$xmldata === NULL ) {
            
$xmldata = &$this->xml_data;
        }
        
$tags    $tags  === NULL   $this->xml_struct $tags;
        foreach ( 
$tags as $tag_name => $clon_tags ) {
            foreach ( 
$clon_tags as $clon_index => $clon_props ) {
                
$sub_tags   $clon_props['sub-tags'];
                
$attributes $clon_props['attributes'];
                
$type       $clon_props['type'];
                
$level      $clon_props['level'];
                
$value      $clon_props['value'];
                
$atts       "";
                
$tab        str_repeat("\t",$level);
                
$top        count($pending_tags);
                for ( 
$i $top$i >=$level$i-- ) {
                    
$levelname  "level_".$i;
                    if ( 
array_key_exists($levelname,$pending_tags) ) {
                        
$xmldata .=  $pending_tags[$levelname];
                        unset(
$pending_tags[$levelname]);
                    }
                }
                
$levelname "level_".$level;
                foreach ( 
$attributes as $att_name => $att_clons ) {
                    foreach ( 
$att_clons as $index_clon => $att_value ) {
                        
$atts .= $att_name.'="'.$att_value.'" ';
                    }
                }
                
$atts = empty($attributes) ? "" " ".$atts;
                
$atts rtrim($atts);
                if ( 
$type == 'open' ) {
                    
$xmldata .= $tab."<".$tag_name.$atts.">".$value."\n";
                    
$pending_tags[$levelname] = $tab."</".$tag_name.">"."\n";
                } else {
                    if ( 
$value == "" ) {
                        
$xmldata .= $tab."<".$tag_name.$atts."/>"."\n";
                    } else {
                        
$xmldata .= $tab."<".$tag_name.$atts.">".$value."</".$tag_name.">"."\n";
                    }
                }
                
$this->createXml($xmldata,$sub_tags,&$pending_tags,$default_level+1);
            }
        }
        
$default_level_name 'level_'.$default_level;
        if ( isset(
$pending_tags[$default_level_name]) ) {
            
$xmldata .= $pending_tags[$default_level_name];
            unset(
$pending_tags[$default_level_name]);
        }
    }
    
    
//saves the data to the file and database
    
public function saveXml($file "") {
        
$file $file == "" $this->xml_file $file;
        
$fp      fopen($file,'w');
        
$bytes   fwrite($fp,$this->xml_data);
        
$file    $this->xml_file;
        
$content serialize($this->xml_struct);
        
$content str_replace('\'',"\"",$content);
        
$sql     "UPDATE config_files SET file_content = '".$content."' WHERE file_name = '".$file."'";
        
$this->db->sqlQuery($sql);
        
fclose($fp);
        if ( 
$bytes !== false) {
            return 
true;
        } else {
            return 
false;
        }
    }
    
    
//get the XML tags in certain level of deepness
    
protected function getXmlLevelTags($level) {
        
$level_tags = array();
        
$cnt        0;
        foreach ( 
$this->xml_values as $index => $props ) {
            if ( 
$props['level'] == $level ) {
                
$level_tags[$cnt]['values']       = $props;
                
$level_tags[$cnt]['global_index'] = $index;
                
$cnt++;
            }
        }
        return 
$level_tags;
    }
    
    
//get the children tags of some other tag
    
protected function getTagChildren($index) {
        
$children     = array();
        
$tag          $this->xml_values[$index];
        
$level        $tag['level'];
        
$target_level $level 1;
        
$index++;
        
$cnt          0;
        while ( 
$this->xml_values[$index]['level'] > $level ) {
            if ( 
$target_level == $this->xml_values[$index]['level'] ) {
                
$children[$cnt]['global_index'] = $index;
                
$children[$cnt]['values']       = $this->xml_values[$index];
                
$cnt++;
            }
            
$index++;
        }
        return 
$children;
    }
    
    
//free the parser
    
public function __destruct() {
        
xml_parser_free($this->xml_parser);
        return 
true;
    }
    

?>
saludos