|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-06-02 00:44 UTC] justinpatrin@php.net
Description:
------------
When trying to set the value (inside text) of a node from an array returned from an xpath call nothing happens. Setting an attribute works fine, however. Setting via normal -> access works as expected.
Reproduce code:
---------------
$xml = simplexml_load_string('<root><node/><node2/></root>');
if (is_array($nodes = $xml->xpath('node'))
&& sizeof($nodes) > 0
&& is_object($nodes[0])) {
$nodes[0]['value'] = 'Hello';
$nodes[0] = 'World';
}
$xml->node2 = 'World';
header('Content-Type: text/xml');
echo $xml->asXML();
Expected result:
----------------
<root><node value="Hello">World</node><node2>World</node2></root>
Actual result:
--------------
<root><node value="Hello"/><node2>World</node2></root>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 21:00:02 2025 UTC |
I've come up with a workaround which imports the SimpleXML obkect into DOM and then converts back, but this, of course, defeats the purpose of SimpleXML. For reference I will post it here for others who need a solution to this problem: $domnode = dom_import_simplexml($xml); $dom = new DOMDocument(); $domnode = $dom->importNode($domnode, true); $dom->appendChild($domnode); $x = new DOMXPath($dom); $n = $x->evaluate('/root/node'); while ($n->item(0)->firstChild) { $n->item(0)->removeChild($n->firstChild); } $n->item(0)->appendChild($dom->createTextNode('World')); $xml = simplexml_import_dom($dom);