J'ai trouve un parser qui repond parfaitement a mon besoin
http://fr2.php.net/manual/fr/ref.xml.php

c'est celui qui utilise $obj->tree


mais je souhaite en faire une classe... fonctionnant sous php4 et php5

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
class Menu {
  var $xml_data;
  var $parser;
  var $xml;
  var $tree;
 
  function Menu() {
    $this->tree = '$this->xml';
  }
 
  function init($file) {
    $fd = fopen($file,"r")
    or die("Impossible d'ouvrir le fichier.");
 
    $this->xml_data = fread($fd,filesize($file));
    fclose($fd);
  }
 
  function initParser() {
    $this->parser = xml_parser_create();
    xml_set_element_handler($this->parser, "$this->startElement", "$this->endElement");
    xml_set_character_data_handler($this->parser, "$this->characterData");
  }
 
  function parse() {
   if (!xml_parse($this->parser, $this->xml_data)) {
       die(sprintf("XML error: %s at line %d",
                   xml_error_string(xml_get_error_code($this->parser)),
                   xml_get_current_line_number($this->parser)));
   }
  }
 
  function startElement($parser, $name, $attrs) {
    // If var already defined, make array
    eval('$test=isset('.$this->tree.'->'.$name.');');
    if ($test) {
      eval('$tmp='.$this->tree.'->'.$name.';');
      eval('$arr=is_array('.$this->tree.'->'.$name.');');
      if (!$arr) {
        eval('unset('.$this->tree.'->'.$name.');');
        eval($this->tree.'->'.$name.'[0]=$tmp;');
        $cnt = 1;
      }
    else {
      eval('$cnt=count('.$this->tree.'->'.$name.');');
    }
 
    $this->tree .= '->'.$name."[$cnt]";
   } else {
     $this->tree .= '->'.$name;
   }
   if (count($attrs)) {
       eval($this->tree.'->attr=$attrs;');
   }
  }
 
  function endElement($parser, $name) {
    // Strip off last ->
    for($a=strlen($this->tree);$a>0;$a--) {
      if (substr($this->tree, $a, 2) == '->') {
        $this->tree = substr($this->tree, 0, $a);
        break;
     }
  function characterData($parser, $data) {
    eval($this->tree.'->data=\''.$data.'\';');
  }
 
  function affich() {
    echo "<pre>\n";
    print_r($this->xml);
    echo "</pre>";
  }
}
 
$menu = new Menu();
 
$menu->init("../menu.xml");
$menu->initParser();
$menu->parse();
$menu->affich();
?>
mais ca ne marche pas

le probleme vient sans doute des $this->nom_fonction definis dans initParser()
mais je ne comprend pas...

Merci par avance.