Ver Mensaje Individual
  #5 (permalink)  
Antiguo 03/04/2007, 16:56
Avatar de GatorV
GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Re: Extraar todo un XML en ARRAY

Para erikrocha:

Su uso:
Código PHP:
<?php
$xml 
= new GeckoXML"personas" );

$persona1 $xml->createNode"persona" );
$persona1->setAttribute"id");
$nombre1 $persona1->createNode"nombre" );
$nombre1->setData"Fulanito" );

$persona2 $xml->createNode"persona" );
$persona2->setAttribute"id");
$nombre2 $persona2->createNode"nombre" );
$nombre2->setData"Sutanito" );

echo 
$xml;
/*
Imprime:
<?xml version="1.0" encoding="utf-8" ?>
<personas>
<persona id="1">
<nombre><![CDATA[Fulanito]]></nombre>
</persona>
<persona id="2">
<nombre><![CDATA[Sutanito]]></nombre>
</persona>
</personas>
*/
?>
La clase:
Código PHP:
<?php
/**
 * GeckoXML
 * 
 * @package com.geckowd.utils;
 * @author Christopher Valderrama <[email protected]>
 * @copyright Copyright (c) 2006
 * @version $Id$v1.0$26 Oct 2006
 * @access public
 **/
class GeckoXML {
    private 
$name;
    private 
$childs;
    private 
$attributes;
    private 
$data;
    private 
$addVer;
    private 
$version;
    
    
/**
     * Constructor
     *
     * Creates a new XML Document/Node
     *
     * If AddV is true, a XML Document is created, otherwise, a node is returned
     *
     * @param string $name
     * @param boolean $addv
     * @access public
     **/
    
public function __construct$name$addv true ) {
        
$this->name $name;
        
$this->childs = array();
        
$this->attributes = array();
        
$this->data "";
        
$this->addVersion false;
        
$this->version "";
        if( 
$addv )
            
$this->addVersion();
    }
    
    
/**
     * addVersion
     *
     * Adds the XML Version and encoding to a XML Document
     *
     * @param string $version XML Version
     * @param string $encoding XML Encoding
     * @access private
     * @return void
     **/
    
private function addVersion$version "1.0"$encoding "utf-8" ) {
        
$this->version "<?xml version=\"$version\" encoding=\"$encoding\" ?>";
        
$this->addVer true;
    }
    
    
/**
     * createNode
     *
     * Creates a new Child Node for the current node/document
     * 
     * @param string $name The Node Name
     * @access public
     * @return GeckoXML A new Node
     **/
    
public function createNode$name ) {
        
$node = new self$namefalse ); 
        
$this->childs[] = $node;
        
        return 
$node;
    }
    
    
/**
     * appendChild
     *
     * DEPRECATED
     *
     * @param GeckoXML node
     * @return GeckoXML node
     * @access public
     **/
    
public function appendChildGeckoXML $node ) {
        
$this->childs[] = $node;
        
        return 
$node;
    }
    
    
/**
     * setAttribute
     *
     * Sets a attribute for this node
     *
     * @param string $name Attribute Name
     * @param string $value Attribute Value
     * @access public
     * @return void
     **/
    
public function setAttribute$name$value ) {
        
$this->attributes[$name] = $value;
    }
    
    
/**
     * setAttributes
     *
     * From a key/value Array, it set's several attributes to a Node
     *
     * @param array $attributes The Attributes array
     * @throws Exception
     * @access public
     * @return void
     **/
    
public function setAttributes$attributes ) {
        if( !
is_array$attributes ) ) {
            throw new 
Exception'$attributes expected to be a array, ' gettype$attributes ) . ' given' );
        }
        
        foreach( 
$attributes as $name => $value ) {
            
$this->setAttribute$name$value );
        }
    }
    
    
/**
     * setData
     * 
     * Set's node inner data, it can cast Arrays, and objects
     *
     * @param mixed $data Node data
     * @throws Exception when no cast is found
     * @access public
     * @return void
     **/
    
public function setData$data ) {
        if( !
is_string$data ) ) {
            
$data $this->varToString$data );
        }
        
        
$this->data "<![CDATA[" $data "]]>";
    }
    
    
/**
     * toString
     *
     * This function converts the object to a XML Representation (magic PHP function)
     *
     * @access public
     * @return string
     **/
    
public 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;
    }
    
    
/**
     * saveXML
     *
     * Saves the XML Object to the Filesystem
     *
     * @param string $fname File Name
     * @access public
     * @throw Exception
     * @return boolean
     **/
    
public function saveXML$fname ) {
        
$fh = @fopen$fname"w" );
        if( !
$fh ) {
            throw new 
Exception"Unable to open " $fname );
        }
        
        
$rst fwrite$fh$this->toString() );
        if( 
$rst === false ) {
            throw new 
Exception"Unable to write to " $fname );
        }
        
        return 
fclose$fh );
    }
    
    
/**
     * streamXML
     *
     * Writes the header, and streams the XML Document
     *
     * @access public
     * @throw Exception
     * @return void
     **/
    
public function streamXML() {
        if( 
headers_sent() ) {
            throw new 
Exception"Headers already sent!, check your code" );
        }
        
        
header"Content-type: text/xml" );
        echo 
$this->toString();
    }
    
    
/**
     * varToString
     *
     * Converts a Variable to a String representation
     *
     * @param mixed $data
     * @access private
     * @return string
     **/
    
private function varToString$data ) {
        if( 
is_array$data ) ) {
            return 
print_r$datatrue );
        }
        
        if( 
is_object$data ) ) {
            return 
get_object_vars$data );
        }
        
        if( 
$data instanceof GeckoXML ) {
            return 
$data->toString();
        }
        
        throw new 
Exception'$data no conversor for this type of var: ' gettype$data ) );
    }
}
?>