title: updatin xml in php
body: <p>hi every one i have an xml file and want to update my code is following</p>
  
  <pre><code>&lt;gallerylist&gt;
    &lt;record&gt;
      &lt;movie&gt;videos/15Avatar_HD.flv&lt;/movie&gt;
      &lt;title&gt;Avatar1&lt;/title&gt;
      &lt;desc&gt;It plays any kind of video file that Flash Player supports&lt;/desc&gt;
      &lt;preview&gt;videos/previews/6avatar.jpg&lt;/preview&gt;
      &lt;imgplaylist&gt;videos/imgplaylist/14p1.jpg&lt;/imgplaylist&gt;
      &lt;category&gt;Action&lt;/category&gt;
    &lt;/record&gt;
  &lt;/gallerylist&gt;
  </code></pre>
  
  <p>and my php function is following:  </p>
  
  <pre><code>function xml_update_video($id,$searchString)
  {
    $res=$this-&gt;selectVideo($id);
    $searchString = $searchString;
    $doc = new DOMDocument;
    $doc-&gt;formatOutput = TRUE;
    $doc-&gt;preserveWhiteSpace = FALSE;
    $doc-&gt;load('../playlist.xml');
    $xPath = new DOMXPath($doc);
    $query = sprintf('//record[./title[text()="%s"]]', $searchString);
  
    foreach($xPath-&gt;query($query) as $node)
    {     
      $node-&gt;parentNode-&gt;removeChild($node);       
    }       
  
    $gallerylist = $doc-&gt;getElementsByTagName( "gallerylist" )-&gt;item(0);        
    $record = $gallerylist-&gt;appendChild($doc-&gt;createElement('record')); 
    $movie = $record-&gt;appendChild($doc-&gt;createElement('movie'));
    $movie-&gt;appendChild($doc-&gt;createTextNode('videos/'.$res['videofile']));  
    $title = $record-&gt;appendChild($doc-&gt;createElement('title'));
    $title-&gt;appendChild($doc-&gt;createTextNode($res['title'])); 
    $desc = $record-&gt;appendChild($doc-&gt;createElement('desc'));
    $desc-&gt;appendChild($doc-&gt;createTextNode($res['description'])); 
    $preview = $record-&gt;appendChild($doc-&gt;createElement('preview'));
    $preview-&gt;appendChild($doc-&gt;createTextNode('videos/previews/'.$res['previewImage']));
    $imgplaylist = $record-&gt;appendChild($doc-&gt;createElement('imgplaylist'));
    $imgplaylist-&gt;appendChild($doc-&gt;createTextNode('videos/imgplaylist/'.$res['playlistImage']));  
    $category = $record-&gt;appendChild($doc-&gt;createElement('category'));  
    $category-&gt;appendChild($doc-&gt;createTextNode($res['category'])); 
    $doc-&gt;formatOutput = true;
    $test1 = $doc-&gt;saveXML(); 
    $doc-&gt;save('../playlist.xml');
   }
  </code></pre>
  
  <p>$id is id of video file in database . with help of id i am adding new node in xml file after deleting node on basis of searchstring which is title.This code is not working properly. it add new node. </p>
  
  <p>this file add new node .which is not right.
  this should be update existing node. but how ? can anyone help</p>