Enable to update node value with DOMElement xml and php

i can read the value of a node but cannot change it. I want to update light node value off by on. This is my xml file.

<root>
    <light>off</light>
    <spray>off</spray>
    <sensor>on</sensor>
    <nutrient>off</nutrient>
</root>

I read the value in light with this and it returns the good value off:

$doc = new DOMDocument();
$doc->load('maincontrol.xml' ) or die("No xml file");
echo $doc->getElementsByTagName('light')->item(0)->nodeValue;

But this does not udpate the value at on, Why?:

$doc->getElementsByTagName('light')->item(0)->nodeValue = "on";
file_put_contents('maincontrol.xml', $doc->saveXML());

1 answer

  • answered 2018-11-07 23:01 Teddol

    The code works well ! The problem came from my xml file permissions on the php server (read only istead read and write).