How to insert data in xml file using php?
- by Nitesh
<?xml version="1.0" encoding="UTF-8"?>
<root></root>
This is my xml file. I want to insert-update data using the dom method in between the tags. I am a beginner in php and Xml technologies. I successfully created and read from this file but not been able to enter data in it using php.
The code for creating is as follows:-
$doc = new DOMDocument('1.0', 'UTF-8');
$ele = $doc->createElement( 'root' );
$ele->nodeValue = $uvar;
$doc->appendChild( $ele );
$test = $doc->save("$id.xml");
The code for reading is as follows:-
$xdoc = new DOMDocument( );
$xdoc->Load("$gid.xml");
$candidate = $xdoc->getElementsByTagName('root')->item(0);
$newElement = $xdoc ->createElement('root');
$txtNode = $xdoc ->createTextNode ($root);
$newElement -> appendChild($txtNode);
$candidate -> appendChild($newElement);
$msg = $candidate->nodeValue;
Can someone help out with inserting and updating. Thank You!