Reputation: 36044
I'm trying to update XML document. I start with a string that contains XML.
I'm loading that string to SimpleXMLElement object:
$xmlDoc = simplexml_load_string($my_xml_string);
I find a node that I would like to update like so:
$node= $xmlDoc->xpath("//nodename[@node_attribute='{$search_attribute_value}']");
Now I would like to update the node_attribute
attribute. I try to do $node['node_attribute']=$new_attribute_value
however $node
is it's own object and this doesn't update the $xmlDoc
object.
How do I find and update attribute value in the $xmlDoc
?
Upvotes: 0
Views: 59
Reputation: 613
Untested, but you should try like this.
$nodename->attributes()->node_attribute = $new_attribute_value;
Upvotes: 0
Reputation: 26385
The $node
you have there is actually an array of nodes. If you know you've only got one to update you can:
$node[0]['node_attribute'] = $new_attribute_value;
More appropriate may be:
$nodes = $xmlDoc->xpath("//nodename[@node_attribute='{$search_attribute_value}']");
foreach ($nodes as $node) {
$node['node_attribute'] = $new_attribute_value;
}
And everything will be updated as expected.
Upvotes: 2