How to insert data in xml file using php?

Posted by Nitesh on Stack Overflow See other posts from Stack Overflow or by Nitesh
Published on 2010-12-28T23:07:39Z Indexed on 2010/12/29 3:54 UTC
Read the original article Hit count: 162

Filed under:
|
|
|
<?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!

© Stack Overflow or respective owner

Related posts about php

Related posts about Xml