Search code examples
phpxmldomdomdocument

How to modify the node values of a XML file located at server end using PHP


I need to modify my xml node value like

<summary>
<data>125</data>
</summary>

to

<summary>
<data>200</data>
</summary>

I am using PHP 5.2. Although i am able to read the xml files, but i am not able to update it

$doc = new DOMDocument();
$doc->load( 'summary.xml' );
$data = $doc->getElementsByTagName( "data" );
$datavalue = $data ->item(0)->nodeValue;
echo "$datavalue\n";

How to modify this data node value?


Solution

  • You can set the nodeValue and then use saveXML():

    $data ->item(0)->nodeValue = 200;
    
    $doc->saveXML();