Ver Mensaje Individual
  #5 (permalink)  
Antiguo 03/08/2011, 10:09
PercevalCiro
 
Fecha de Ingreso: junio-2009
Mensajes: 64
Antigüedad: 14 años, 10 meses
Puntos: 6
Respuesta: Parsear un Array a Un XML.

Excelente, Necroside, la voy a probar ... yo encontre otra.. y me ha funcionado muy bien tambien...

Por si alguien necesita:

Código PHP:
<?
class ClassXmlArray{
        var 
$encoding'ISO-8859-1';
    var 
$version '1.0';
    var 
$html     false;
    var 
$ln         '';
 
    public function 
array2xml($arr,$file=NULL)
    {     
$xml ='<?xml version="'.$this->version.'" encoding="'.$this->encoding.'" ?>'.$this->ln;
         
$xml.='<rows>';
        
$xml.=$this->arrXml($arr);
        
$xml.='</rows>';
        if(
$file && ($fp=fopen($file,'w')))
        {     
fwrite($fp,$xml); 
            
fclose($fp);  
            return 
true
        }else return 
$xml;
    }
 
    public function 
arrXml($ar)
    {    if(
is_array($ar))
        {    if(
count($ar))
            {    
$xml='';
                foreach(
$ar as $k=>$v)
                {    
$p='';                
                    
$key=is_numeric($k)?'row':$k;
                    if(
is_array($v))
                    {    foreach(
$v as $pk=>$pv)//Propiedades
                        
{    if($pk{0}=='@')
                            {    
$p.=' '.str_replace('@','',$pk).'="'.($pv).'"';
                                unset(
$v[$pk]);
                            }                        
                        } 
$tm=$this->arrXml($v);                                
                    }else 
$tm=$v;
                    
$xml.="<$key$p>".($tm)."</$key>";                                            
                }return 
$xml;    
            }else return 
'';    
        }else return 
$ar;
    }
    public function 
xml2array($file,$html=true)
    {    
$this->html=$html;
        if(
file_exists($file))
        {    
$xxm=simplexml_load_file($file);
            
//return $this->xml2arr($xxm);
            
return $this->_xml2Array($xxm);
            
//print_r($this->_xml2Array($xxm));
        
}else return false;
    }    
 
    public function 
_xml2Array($xml,$attribsAsElements=0)
    {  if (
get_class($xml) == 'SimpleXMLElement'
       {   
$attributes $xml->attributes();
           foreach(
$attributes as $k=>$v) {  if ($v$a[$k] =(string)$v; }
           
$x $xml;
           
$xml get_object_vars($xml);
       }   
       if (
is_array($xml)) 
       {   if (
count($xml) == 0) return (string)$x;
           foreach(
$xml as $key=>$value
           {   
$r[$key] = $this->_xml2Array($value,$attribsAsElements);
               if (!
is_array($r[$key])) $r[$key] = (string)$r[$key];                
           }
           if (isset(
$a)) 
           {  if(
$attribsAsElements$r array_merge($a,$r);
              else 
              {    foreach(
$a as $k=>$v)$r['@'.$k]=(string)$v;                                                                       
                unset(
$r['@attributes']);               
              }
           }
           return 
$r;
       }
       return (string) 
$xml;
    } 
}

//Implementando Prueba 002
$prueba = new ClassXmlArray();
$array = array("nombre" => array("p" =>'nombre1'"e" =>'nombre2'), "nombre2" => array("p" =>'nombre3'"e" =>'nombre4'));
$prueba->array2xml($array,"archi.xml");
?>
Donde el resultado es: el archivo archi.xml generado y:
Código HTML:
<rows><nombre>
<p>nombre1</p>
<e>nombre2</e>
</nombre><nombre2>
<p>nombre3</p>
<e>nombre4</e>
</nombre2>
</rows> 

Muchas Gracias ... Necroside. Despues te comento.

A esta clase le agregue tambien "protected $nodo_principal = 'rows'; " para que pueda seleccionar el nombre del cuerpo.

Última edición por PercevalCiro; 03/08/2011 a las 10:23