Ver Mensaje Individual
  #4 (permalink)  
Antiguo 14/01/2009, 08:52
Avatar de anlhp
anlhp
 
Fecha de Ingreso: agosto-2008
Mensajes: 121
Antigüedad: 15 años, 8 meses
Puntos: 1
yaaa, una lastima :s, tendre que pensar mas xD jejeje, nada explico, es que llevo un tiempo trabajando con xml en php y hasta hoy creaba una funcion por cada cosa que queria hacer con xml y de pronto digo: mierda >< por que no hago una clase y ya esta? y en eso estoy, les dejo lo unico que he hecho, esta pensada mas bien para llevar una sencilla galeria de fotos en xml pero se me han ocurrido muchas mas cosas que se le puede agregar, pero weno, por ahora es lo que hay ;)
Código PHP:
<?php

/**
 * @author anler
 * @copyleft 2009
 */

class XMLimg{
    private 
$images;
    private 
$xml;
    private 
$path;
    
    public function 
create($path 'untitled.xml'$root 'images'$override true$version '1.0'){
        if(
file_exists($path) && !$override)
            return -
1;
        try{
            if(!
$this->xml domxml_new_doc($version))
                throw new 
Exception('Could not create/override the file.');
            
$this->xml->append_child(new DOMElement($root));
            
$this->path $path;
        }catch(
exception $e){
            echo 
$e->getMessage();    
        }
    }
    
    public function 
open($path){
        try{
            if(!
$this->xml domxml_open_file($path))
                throw new 
Exception('Could not open the file.');
        }catch(
exception $e){
            echo 
$e->getMessage();
        }
    }
    
    public function 
add($_element 'image'){
        
$root $this->xml->document_element();
        
$num_args func_num_args();
        
$element = new DOMElement($_element);
        for(
$i 1$i < (($num_args%2)?$num_args-1:$num_args); $i+=2){
            
$attr func_get_arg($i);
            
$value func_get_arg($i+1);
            
$element->set_attribute($attr$value);
        }
        
$root->append_child($element);
    }
    
    public function 
delete(){
        
    }
    
    public function 
save_as($name 'untitled.xml'){
        try{
            if(!
$this->xml->dump_file($nametruefalse))
                throw new 
Exception('No se puede guardar el archivo.');
        }catch(
exception $e){
            echo 
$e->getMessage();
        }
    }
    
    public function 
save(){
        try{
            if(
$this->xml && !$this->xml->dump_file($this->pathtruefalse))
                throw new 
Exception('No se puede guardar el archivo.');
            else
                return -
1;
        }catch(
exception $e){
            echo 
$e->getMessage();
        }
    }
}
y el tema de los parametros opcionales es porque si pudiese llamar asi al metodo XMLimg::add(,"url", "imagen.jpg", "descripcion", "esta es la descripcion de la imagen")
seria muy sencillo y no obligaria a poner un nombre de etiqueta para cada nueva imagen en el xml (pues seria <image /> por defecto) porque no quiero 'ensuciar' mas el metodo para saber si el ultimo parametro que me pasan es el nombre de la etiqueta

la parte del for salio un poco jodida, pal que le interese es esto
for($i = 1; $i < (($num_args%2)?$num_args-1:$num_args); $i+=2)

Última edición por GatorV; 14/01/2009 a las 10:50