Ver Mensaje Individual
  #13 (permalink)  
Antiguo 11/04/2007, 08:30
Avatar de GatorV
GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 18 años
Puntos: 2135
Re: Generar XML con PHP

Encontre una version anterior de mi framework (para PHP4) asi que te puede servir, lo unico malo es que a la hora de generar nodos, necesitas agregarlos con append child:
Código PHP:
$aNode $xml->createNode("alumnos");
$aNode->setData("yo");
$xml->appendChild($aNode); 
Aqui esta la clase:
Código PHP:
<?php
class GeckoXML {
    var 
$name;
    var 
$childs;
    var 
$attributes;
    var 
$data;
    var 
$addVer;
    var 
$version;
    
    function 
GeckoXML$name$addv true ) {
        
$this->name $name;
        
$this->childs = array();
        
$this->attributes = array();
        
$this->data "";
        
$this->addVersion false;
        
$this->version "";
        if( 
$addv )
            
$this->addVersion();
    }
    
    function 
addVersion$version "1.0"$encoding "utf-8" ) {
        
$this->version "<?xml version=\"$version\" encoding=\"$encoding\" ?>";
        
$this->addVer true;
    }
    
    function 
createNode$name ) {
        return new 
GeckoXML$namefalse );
    }
    
    function 
appendChild$node ) {
        
$this->childs[] = $node;
        
        return 
$node;
    }
    
    function 
setAttribute$name$value ) {
        
$this->attributes[$name] = $value;
    }
    
    function 
setAttributes$attributes ) {
        foreach( 
$attributes as $name => $value ) {
            
$this->setAttribute$name$value );
        }
    }
    
    function 
setData$data ) {
        
$this->data "<![CDATA[" $data "]]>";
    }
    
    function 
toString() {
        
$name $this->name;
        
$data $this->data;
        
$xml "";
        
$attrs "";
        
        if( 
count$this->attributes ) > ) {
            foreach( 
$this->attributes as $aname => $avalue ) {
                
$attrs .= " $aname=\"$avalue\"";
            }
        }
        
        if( empty( 
$data ) && ( count$this->childs ) > ) ) { // Node tag get childs
            
$begintag "<$name$attrs>";
            
$endtag "</$name>";
            foreach( 
$this->childs as $child ) {
                
$xml .= $child->toString();
            }
            
$xml $begintag $xml $endtag;
        } else {
            if( empty( 
$data ) ) { // empty node
                
$xml "<$name$attrs />";
            } else {
                
$xml "<$name$attrs>$data</$name>";
            }
        }
        
        if( 
$this->addVer ) {
            
$xml $this->version $xml;
        }
        
        return 
$xml;
    }
    
    function 
saveXML$fname ) {
        
$fh = @fopen$fname"w" );
        if( !
$fh ) return false;
        
        
$rst fwrite$fh$this->toString() );
        if( 
$rst === false ) return false;
        
        return 
fclose$fh );
    }
}
?>