php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #48916 Add feature to delete XML node in SimpleXML when node selected by xpath
Submitted: 2009-07-14 14:53 UTC Modified: 2009-07-14 15:41 UTC
Votes:4
Avg. Score:5.0 ± 0.0
Reproduced:4 of 4 (100.0%)
Same Version:4 (100.0%)
Same OS:3 (75.0%)
From: piotr dot baranowski at zeno dot pl Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.2.10 OS: All of them
Private report: No CVE-ID: None
 [2009-07-14 14:53 UTC] piotr dot baranowski at zeno dot pl
Description:
------------
Just as in the title. There's no way now to delete XML node in SimpleXML. It would be nice to have SimpleXML method that would delete given node.

Reproduce code:
---------------
---
From manual page: book.simplexml
---



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-14 15:09 UTC] rrichards@php.net
no need for additional method. unset() performs deletions
 [2009-07-14 15:41 UTC] piotr dot baranowski at zeno dot pl
Sorry, i forgot to write that this is about nodes selected by xpath ;-)
Yeah, i know, so lame of me.

<?php
$xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?>
<root>
<recipes>
<recipe>
	<id>1</id>
</recipe>
<recipe>
	<id>2</id>
</recipe>
</recipes>
</root>');
print_r($xml);

$node = $xml->xpath('recipes/recipe[id="1"]');
$foo = &$node[0];

unset($node[0]);
$node[0] = null;

unset($foo);
$foo = null;

print_r($xml);
?>

print_r gives same result before and after unset or setting node to null. It's because only the reference, not content of reference, is unset().

There's no way to delete (or no way that i could thought about) to delete node selected by xpath.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC