Re: SimpleXML strange behaviour.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2008/9/9 Mirco Soderi <m.soderi@xxxxxxxx>

> 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

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux