[PHP] SimpleXML strange behaviour.

2008-09-09 Thread Mirco Soderi
Do you know any reason why the following code does work


$xmlOperazioni = simplexml_load_file($xmlFilename);
foreach($xmlOperazioni-operazione as $operazione) {
   if($operazione['nome'] == $operazioneRichiesta) {
  require($operazione['php']);
  break;
   }
}

but the following does not

$xmlOperazioni = 
simplexml_load_file($impostazioni['root']['configurazione']['generale']['xml_operazioni']);
foreach($xmlOperazioni-operazione as $operazione) {
   if($operazione-nome == $operazioneRichiesta) {
  require($operazione-php);
  break;
   }
}

that is I MUST access childrens of the element operazione as if it was an 
array, while (in the same page) the following code does work

$xmlViste = 
simplexml_load_file($impostazioni['root']['configurazione']['generale']['xml_composizione_viste']);
foreach($xmlViste-vista as $vista) {
if($vista-nome == $nomeVistaDaMostrare) {
   $smarty-assign(intestazione,componenti/.$vista-intestazione);
   $smarty-assign(menu,componenti/.$vista-menu);
   $smarty-assign(contenuto, componenti/.$vista-contenuto);
   $smarty-assign(pie_di_pagina, componenti/.$vista-pie_di_pagina);
   break;
}
}

but the following does not

$xmlViste = 
simplexml_load_file($impostazioni['root']['configurazione']['generale']['xml_composizione_viste']);
foreach($xmlViste-vista as $vista) {
if($vista['nome'] == $nomeVistaDaMostrare) {
   $smarty-assign(intestazione,componenti/.$vista['intestazione']);
   $smarty-assign(menu,componenti/.$vista['menu']);
   $smarty-assign(contenuto, componenti/.$vista['contenuto']);
   $smarty-assign(pie_di_pagina, componenti/.$vista['pie_di_pagina']);
   break;
}
}

that is this time I MUST access the childrens of the element operazione as if 
it was an object?

Re: [PHP] SimpleXML strange behaviour.

2008-09-09 Thread Nathan Nobbe
2008/9/9 Mirco Soderi [EMAIL PROTECTED]

 Do you know any reason why the following code does work


 $xmlOperazioni = simplexml_load_file($xmlFilename);
 foreach($xmlOperazioni-operazione as $operazione) {
   if($operazione['nome'] == $operazioneRichiesta) {
  require($operazione['php']);
  break;
   }
 }

 but the following does not

 $xmlOperazioni =
 simplexml_load_file($impostazioni['root']['configurazione']['generale']['xml_operazioni']);
 foreach($xmlOperazioni-operazione as $operazione) {
   if($operazione-nome == $operazioneRichiesta) {
  require($operazione-php);
  break;
   }
 }


well im guessing thats because youre using the same file and different
accessors, what i mean is that if
$xmlFilename, and
$impostazioni['root']['configurazione']['generale']['xml_operazioni'] refer
to the same xml from above, then obviously this wont work..

this is looking for an attribute, 'nome', $operazione['nome'];
this is looking for a child element, 'nome', $operazione-nome

that is I MUST access childrens of the element operazione as if it was an
 array, while (in the same page) the following code does work

 $xmlViste =
 simplexml_load_file($impostazioni['root']['configurazione']['generale']['xml_composizione_viste']);
 foreach($xmlViste-vista as $vista) {
if($vista-nome == $nomeVistaDaMostrare) {
   $smarty-assign(intestazione,componenti/.$vista-intestazione);
   $smarty-assign(menu,componenti/.$vista-menu);
   $smarty-assign(contenuto, componenti/.$vista-contenuto);
   $smarty-assign(pie_di_pagina,
 componenti/.$vista-pie_di_pagina);
   break;
}
 }

 but the following does not

 $xmlViste =
 simplexml_load_file($impostazioni['root']['configurazione']['generale']['xml_composizione_viste']);
 foreach($xmlViste-vista as $vista) {
if($vista['nome'] == $nomeVistaDaMostrare) {
   $smarty-assign(intestazione,componenti/.$vista['intestazione']);
   $smarty-assign(menu,componenti/.$vista['menu']);
   $smarty-assign(contenuto, componenti/.$vista['contenuto']);
   $smarty-assign(pie_di_pagina,
 componenti/.$vista['pie_di_pagina']);
   break;
}
 }


again, this syntax denotes a child element,
$vista-nome
this syntax denotes an attribute
$vista['nome']

that is this time I MUST access the childrens of the element operazione as
 if it was an object?


i recommend a review of the example page on simple xml, but yes children are
access via the - notation, you can also use the children() method if its
more convenient for you.

http://us2.php.net/manual/en/simplexml.examples.php

-nathan