Re: Re: remove SimpleXML nodes

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

 



Thanks a lot to all :)

Using DOM I can do what I need. Anyway I was trying the proposed solutions
with SimpleXML but none of them worked for me :(

I tried splitting $key and $value in the foreach command, but when I dump
the xml (with the asXML function) I still have the nodes that I supposed to
delete... And the same using references, cause I think using unset with
referenced variable names doesn't delete the referenced var, it just deletes
the link to the referenced var...

Concretely I tried:

TEST ONE:
foreach ($xmlDatabase as $key => $oneTable) if ($oneTable['name'] == 'one')
unset($xmlDatabase[$key]);
$sourceXML = $xmlDatabase->asXML(); // <----- node <table name='one'> is
still there

TEST TWO
foreach ($xmlDatabase as $key => &$oneTable) if ($oneTable['name'] == 'one')
unset($oneTable);
$sourceXML = $xmlDatabase->asXML(); // <----- node <table name='one'> is
still there


If there's no luck with xml I will use of course DOM for this, but I'm
really curious now :P
Again, thanks a lot for the help.

Javi Ruiz.

On 9/1/06, Curt Zirzow <czirzow@xxxxxxxxx> wrote:
On 8/31/06, Adam Zey <azey@xxxxxx> wrote:
Javier Ruiz wrote:
> Hi all,
>
> So I want to do...
>
> $xmlDatabase = new SimpleXMLElement($myXML);
> foreach ($xmlDatabase as $oneTable)
> {
>   if ($oneTable['name'] == 'two')
>   {
>      ///  ---- HERE I WANT TO DELETE THE $oneTable NODE
>      unset($oneTable);   // <-- and this doesn't work...
>   }
> }
>
>
> any ideas?
>

The answer is simple, everybody else seems to have missed it. foreach()
makes a copy of the array before working on it. Changes made to
$oneTable would obviously never affect the original. You're just
unsetting a temporary variable that's going to be overwritten next time
through the loop anyhow.

You should have better luck with this code:

$xmlDatabase = new SimpleXMLElement($myXML);
foreach ($xmlDatabase as $key => $oneTable)

fwiw, In php5 you can do something like:

foreach ($xmlDatabase as $key => &$oneTable)
                //note the & ------------------^^^^
 unset($oneTable);

[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