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 83 84 85 86
| <?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_object($this->parser,&$this);
xml_set_element_handler($this->parser, "startElement", "endElement");
xml_set_character_data_handler($this->parser, "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();
?> |
Partager