SimpleXMLIterator

SimpleXMLIterator - PHP - Programmation

Marsh Posté le 16-12-2005 à 18:34:10    

Lut, qqn a déja utilisé cette classe (Php5) ?
 
J'essaye d'utiliser un arbre (sauvegardé sous forme d'xml) :
 

Code :
  1. <?xml version="1.0" ?>
  2. <arbre nom="Racine">
  3.     <fils nom="Noeud">
  4.         <fils nom="Sous-Noeud"/>
  5.         <fils nom="Sous-Noeud"/>
  6.     </fils>
  7.     <fils nom="Noeud2">
  8.         <fils nom="Sous-Noeud2"/>
  9.         <fils nom="Sous-Noeud2"/>
  10.     </fils>
  11. </arbre>


 
J'essaye d'utiliser SimpleXMLIterator (je dois fournir un iterateur héritant de RecursiveIterator à la méthode factory generant l'arbre or SimpleXMLIterator herite deja de RecursiveIterator donc ça tombe bien :D) pour parcourir cet arbre mais le problème c'est que les méthodes hasChildren() et getChildren() retourne toujours faux donc je reste au niveau de "Noeud" (pas de "Sous-Noeud" ). Ci dessous, la méthode qui utilise cet itérateur pour construire l'arbre. Tree_Factory_Iterator est simplement un RecursiveIterator qui ajoute une méthode getTag() (retourne la valeur de l'attribut "nom" dans mon cas).
 

Code :
  1. public static function factory(Tree_Factory_Iterator $it, $node = null)
  2.     {
  3.         if (is_null($node)) {
  4.             $node = new Tree();
  5.         }
  6.        
  7.         foreach ($it as $v) {
  8.            
  9.             $newNode = $node->nodes->add(new Tree_Node($v->getTag()));
  10.          
  11.             if ($v->hasChildren()) {
  12.                 Tree::factory($v->getChildren(), $newNode);
  13.             }
  14.         }
  15.        
  16.         return $node;
  17.     }


 
Si quelqu'un à la moindre idée...  :hello:  
Merci d'avance
 
Pour info l'interface à respecter Tree_Factory_Iterator :
 

Code :
  1. interface Tree_Factory_Iterator extends RecursiveIterator
  2. {
  3.     /**
  4.     * Returns tag data which the Tree::factory()
  5.     * method uses.
  6.     */
  7.     public function getTag();
  8. }


 
et ma classe de manip de l'XML (echo principalement pour debuggage) :
 

Code :
  1. class Tree_Factory_XML implements Tree_Factory_Iterator
  2. {
  3.     private $xmlIt;
  4.    
  5.     public function getTag()
  6.     {
  7.         return $this->xmlIt["nom"];
  8.     }
  9.    
  10.     public function __construct(SimpleXMLIterator $xml)
  11.     {
  12.        $this->xmlIt = $xml;
  13.        echo ("construct ".$this->xmlIt["nom"]."<br>" );
  14.        //$this->rewind();
  15.     }
  16.     /**
  17.     * Iterator::current()
  18.     */
  19.     public function current()
  20.     {
  21.        echo ("current ".$this->xmlIt["nom"]."<br>" );
  22.        $temp = $this->xmlIt->current();
  23.        echo ("current2".$temp["nom"]."<br>" );
  24.      
  25.        return new Tree_Factory_XML($this->xmlIt->current());
  26.     }
  27.     /**
  28.     * Iterator::key()
  29.     */
  30.     public function key()
  31.     {
  32.         echo ("key ".$this->xmlIt["nom"]."<br>" );
  33.         $this->xmlIt->key();
  34.     }
  35.     /**
  36.     * Iterator::next()
  37.     */
  38.     public function next()
  39.     {
  40.         echo ("next ".$this->xmlIt["nom"]."<br>" );
  41.         $this->xmlIt->next();
  42.     }
  43.     /**
  44.     * Iterator::rewind()
  45.     */
  46.     public function rewind()
  47.     {
  48.         echo ("rewind ".$this->xmlIt["nom"]."<br>" );
  49.         $this->xmlIt->rewind();
  50.     }
  51.     /**
  52.     * Iterator::valid()
  53.     */
  54.     public function valid()
  55.     {
  56.         echo ("valid ".$this->xmlIt["nom"]."<br>" );
  57.         return $this->xmlIt->valid();
  58.     }
  59.     /**
  60.     * Iterator::hasChildren()
  61.     */
  62.     public function hasChildren()
  63.     {
  64.         echo ("hasChildren ".$this->xmlIt["nom"]." ". $this->xmlIt->hasChildren()."<br>" );
  65.         return $this->xmlIt->hasChildren();
  66.     }
  67.     /**
  68.     * Iterator::getChildren()
  69.     */
  70.     public function getChildren()
  71.     {
  72.         echo ("getChildren ".$this->xmlIt["nom"]."<br>" );
  73.         return $this->xmlIt->getChildren();
  74.     }
  75. }


Message édité par papy_danone le 16-12-2005 à 18:40:03
Reply

Marsh Posté le 16-12-2005 à 18:34:10   

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed