Re: [PHP] Re: remove SimpleXML nodes

2006-09-01 Thread Adam Zey
It seems there's no way to do this. You can unset certain things in 
simpleXML objects, but it's really strange non-standard behaviour. If I 
have this for an XML document:




  foo
  bar
  baz

XML;

and I run that through simplexml into $xml, then doing unset($xml->test) 
and then exporting again yields this:







Also, if I echo $xml->test[1], I get "bar". But I can't unset anything 
like that. $xml->test says it's an array through var dumps, but it 
doesn't seem to be a real array, only a fake array.


I'm tempted to say it can't be done. One thing you might try is, to do 
the unset, export to DOM using dom_import_simplexml(), use DOM to unset 
the thing, and then use simplexml_import_dom(). Who knows how slow 
that'd be, though.


Regards, Adam.

Javier Ruiz wrote:

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  is
still there

TEST TWO
foreach ($xmlDatabase as $key => &$oneTable) if ($oneTable['name'] == 
'one')

unset($oneTable);
$sourceXML = $xmlDatabase->asXML(); // <- node  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.



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: remove SimpleXML nodes

2006-09-01 Thread Javier Ruiz

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  is
still there

TEST TWO
foreach ($xmlDatabase as $key => &$oneTable) if ($oneTable['name'] == 'one')
unset($oneTable);
$sourceXML = $xmlDatabase->asXML(); // <- node  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 <[EMAIL PROTECTED]> wrote:
On 8/31/06, Adam Zey <[EMAIL PROTECTED]> 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);


Re: [PHP] Re: remove SimpleXML nodes

2006-08-31 Thread Curt Zirzow

On 8/31/06, Adam Zey <[EMAIL PROTECTED]> 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);

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: remove SimpleXML nodes

2006-08-31 Thread Adam Zey

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)
{
  if ($oneTable['name'] == 'two')
  {
 ///   HERE I WANT TO DELETE THE $oneTable NODE
 unset($xmlDatabase[$key]);
  }
}

What that is doing is having foreach also get the key of the array 
element that $oneTable represents. It then does the unset() on the 
original table instead of the copy. Remember that all array elements 
have keys, even if you didn't set one or it doesn't look like they 
should. In this case, you don't know what the key is internally, and you 
don't care either.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php