Re: Re: remove SimpleXML nodes

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

 



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:

<?xml version='1.0'?>
<document>
  <test>foo</test>
  <test>bar</test>
  <test>baz</test>
</document>
XML;

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

<?xml version='1.0'?>
<document>

</document>

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 <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.


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


[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