Ainsi, je me suis mis à créer un objet qui parse une feuille XML et qui en retire la configuration du script, environement ou module concerné. Voilà le script en procédural :
CODE
<?
$xml_file='Config.xml';
function open_markup($parser, $markup, $attributes)
{
global $current_vargroup, $xml_tree;
if($markup=='VARGROUP')
{
$current_vargroup=$attributes['NAME'];
$xml_tree[$current_vargroup] = array();
}
if(($current_vargroup)&&($markup=='VAR'))
{
$xml_tree[$current_vargroup][$attributes['NAME']]=$attributes['VALUE'];
echo $xml_tree[$current_vargroup][$attributes['NAME']] .' ';
}
}
function close_markup($parser, $markup)
{
if($markup=='VARGROUP')
{
global $current_vargroup;
$current_vargroup=false;
}
}
$xml_tree = array();
$current_vargroup=false;
$parser = xml_parser_create();
xml_set_element_handler($parser, 'open_markup', 'close_markup');
//xml_set_character_data_handler($parser, 'Texte');
$f = fopen($xml_file, 'r');
while ($line = fgets($f, 1024))
{
xml_parse($parser, $line, feof($f));
}
xml_parser_free($parser);
fclose($f);
echo '<dl>';
foreach($xml_tree as $key=>$value)
{
echo '<dt>' . $key . '</dt>';
foreach($value as $key2=>$value2)
{
echo '<dd>' . $key2 . ' = ' . $value2 . '</dd>';
}
}
echo '</dl>';
?>
$xml_file='Config.xml';
function open_markup($parser, $markup, $attributes)
{
global $current_vargroup, $xml_tree;
if($markup=='VARGROUP')
{
$current_vargroup=$attributes['NAME'];
$xml_tree[$current_vargroup] = array();
}
if(($current_vargroup)&&($markup=='VAR'))
{
$xml_tree[$current_vargroup][$attributes['NAME']]=$attributes['VALUE'];
echo $xml_tree[$current_vargroup][$attributes['NAME']] .' ';
}
}
function close_markup($parser, $markup)
{
if($markup=='VARGROUP')
{
global $current_vargroup;
$current_vargroup=false;
}
}
$xml_tree = array();
$current_vargroup=false;
$parser = xml_parser_create();
xml_set_element_handler($parser, 'open_markup', 'close_markup');
//xml_set_character_data_handler($parser, 'Texte');
$f = fopen($xml_file, 'r');
while ($line = fgets($f, 1024))
{
xml_parse($parser, $line, feof($f));
}
xml_parser_free($parser);
fclose($f);
echo '<dl>';
foreach($xml_tree as $key=>$value)
{
echo '<dt>' . $key . '</dt>';
foreach($value as $key2=>$value2)
{
echo '<dd>' . $key2 . ' = ' . $value2 . '</dd>';
}
}
echo '</dl>';
?>
Ceci fonctionne correctement, le but étant, de récupérer des paramètres et de les intégrer au tableau nommé $xml_tree. Je souhaite intégrer ce script à une méthode d'un objet nommé configuration, mais le problème est que celui-ci ne fonctionne plus, enfin, celui-ci ne passe pas le tableau $xml_tree à l'objet qui possède pourtant un attribut $Config. La méthode constructeur est censée parser le fichier à l'aide de ce script et retourner le tableau $xml_tree dans l'attribut $Config... Mais ça marche pô... Que faire ?
Annexes :
Fichier XML :
CODE
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE confml PUBLIC "-//XCMS//DTD CONFML 1.0 //EN" "http://groupeclan.free.fr/DTD/confml.dtd">
<confml>
<vargroup name="properties">
<var name="errorlog" value="false" />
<var name="actionlog" value="false" />
</vargroup>
<vargroup name="settings">
<var name="datasource" value="" />
<var name="display" value="" />
<var name="selection" value="" />
</vargroup>
<vargroup name="constants">
<var name="location" value="http://127.0.0.1/XCMS2/" />
</vargroup>
<vargroup name="parameters">
<var name="id" value="" />
<var name="lang" value="fr" />
<var name="stylesheet" value="basic" />
<var name="doctype" value="xhtml" />
</vargroup>
<vargroup name="rights">
<var name="access" value="5" />
</vargroup>
</confml>
<!DOCTYPE confml PUBLIC "-//XCMS//DTD CONFML 1.0 //EN" "http://groupeclan.free.fr/DTD/confml.dtd">
<confml>
<vargroup name="properties">
<var name="errorlog" value="false" />
<var name="actionlog" value="false" />
</vargroup>
<vargroup name="settings">
<var name="datasource" value="" />
<var name="display" value="" />
<var name="selection" value="" />
</vargroup>
<vargroup name="constants">
<var name="location" value="http://127.0.0.1/XCMS2/" />
</vargroup>
<vargroup name="parameters">
<var name="id" value="" />
<var name="lang" value="fr" />
<var name="stylesheet" value="basic" />
<var name="doctype" value="xhtml" />
</vargroup>
<vargroup name="rights">
<var name="access" value="5" />
</vargroup>
</confml>
Classe configuration :
CODE
<?
class Configuration
{
function Configuration()
{
$this->Report($this->getConfig('Config.xml'));
}
// Get the configuration's files
function getConfig($xml_file)
{
function open_markup($parser, $markup, $attributes)
{
global $current_vargroup, $xml_tree;
if($markup=='VARGROUP')
{
$current_vargroup=$attributes['NAME'];
$xml_tree[$current_vargroup] = array();
}
if(($current_vargroup)&&($markup=='VAR'))
{
$xml_tree[$current_vargroup][$attributes['NAME']]=$attributes['VALUE'];
echo $xml_tree[$current_vargroup][$attributes['NAME']] .' ';
}
}
function close_markup($parser, $markup)
{
if($markup=='VARGROUP')
{
global $current_vargroup;
$current_vargroup=false;
}
}
$xml_tree = array();
$current_vargroup=false;
$parser = xml_parser_create();
xml_set_element_handler($parser, 'open_markup', 'close_markup');
$f = fopen($xml_file, 'r');
while ($line = fgets($f, 1024))
{
xml_parse($parser, $line, feof($f));
}
xml_parser_free($parser);
fclose($f);
return $xml_tree;
}
function Report($xml_tree)
{
echo '<dl>';
foreach($xml_tree as $key=>$value)
{
echo '<dt>' . $key . '</dt>';
foreach($value as $key2=>$value2)
{
echo '<dd>' . $key2 . ' = ' . $value2 . '</dd>';
}
}
echo '</dl>';
}
}
$hohop = new Page();
?>
class Configuration
{
function Configuration()
{
$this->Report($this->getConfig('Config.xml'));
}
// Get the configuration's files
function getConfig($xml_file)
{
function open_markup($parser, $markup, $attributes)
{
global $current_vargroup, $xml_tree;
if($markup=='VARGROUP')
{
$current_vargroup=$attributes['NAME'];
$xml_tree[$current_vargroup] = array();
}
if(($current_vargroup)&&($markup=='VAR'))
{
$xml_tree[$current_vargroup][$attributes['NAME']]=$attributes['VALUE'];
echo $xml_tree[$current_vargroup][$attributes['NAME']] .' ';
}
}
function close_markup($parser, $markup)
{
if($markup=='VARGROUP')
{
global $current_vargroup;
$current_vargroup=false;
}
}
$xml_tree = array();
$current_vargroup=false;
$parser = xml_parser_create();
xml_set_element_handler($parser, 'open_markup', 'close_markup');
$f = fopen($xml_file, 'r');
while ($line = fgets($f, 1024))
{
xml_parse($parser, $line, feof($f));
}
xml_parser_free($parser);
fclose($f);
return $xml_tree;
}
function Report($xml_tree)
{
echo '<dl>';
foreach($xml_tree as $key=>$value)
{
echo '<dt>' . $key . '</dt>';
foreach($value as $key2=>$value2)
{
echo '<dd>' . $key2 . ' = ' . $value2 . '</dd>';
}
}
echo '</dl>';
}
}
$hohop = new Page();
?>