title: PHP and XML - Deleting nodes
body: <p>I have created a PHP script which builds a table from an XML document via AJAX. For example:</p>
  
  <pre><code>&lt;bookstore&gt;
    &lt;book&gt;
      &lt;title&gt;Everyday Italian&lt;/title&gt;
      &lt;author&gt;Giada De Laurentiis&lt;/author&gt;
      &lt;year&gt;2005&lt;/year&gt;
      &lt;price&gt;30.00&lt;/price&gt;
    &lt;/book&gt;
    &lt;book&gt;
      &lt;title&gt;Harry Potter&lt;/title&gt;
      &lt;author&gt;J K.  Rowling&lt;/author&gt;
      &lt;year&gt;2005&lt;/year&gt;    
      &lt;price&gt;29.99&lt;/price&gt;
    &lt;/book&gt;
  &lt;/bookstore&gt;
  </code></pre>
  
  <p>Would create a table with title, author, year and price columns plus an additional delete colum.  When parsing the XML I have set the tr id to be that of the current XML element (0 and 1) in the case above. </p>
  
  <p>When I click delete I fire off an AJAX request with the ID of the row I want to delete.  The delete script is receiving the current row number without problem but I am having strange results when trying to delete it.  The current code I'm trying is below (taken from <a href="http://quest4knowledge.wordpress.com/2010/09/04/php-xml-create-add-edit-modify-using-dom-simplexml-xpath/" rel="nofollow">http://quest4knowledge.wordpress.com/2010/09/04/php-xml-create-add-edit-modify-using-dom-simplexml-xpath/</a> 7.2)</p>
  
  <pre><code>if (isset($_POST['rowNumber'])) {
  
      $rowNumber = $_POST['rowNumber'];
      $file = $_POST['file'];
  
      $dom = new DOMDocument();
      $dom-&gt;load("../XML/".$file);      
      $xml = $dom-&gt;documentElement;
      //PROBLEM HERE
      $xml-&gt;childNodes-&gt;item(0)-&gt;parentNode-&gt;removeChild($xml-&gt;childNodes-&gt;item($rowNumber));
  
      $handle = fopen("../XML/".$file, 'w');
      fwrite($handle, $dom-&gt;saveXML()); 
  
  }
  </code></pre>
  
  <p>I build the table on page load and then on every deletion. Trouble is that the incorrect rows are being deleted and I can't figure out why.</p>
  
  <p><em><strong>ADDITIONAL TESTING...</em></strong></p>
  
  <p>Deletes the first node on 50% of clicks:</p>
  
  <pre><code>$xml-&gt;childNodes-&gt;item(0)-&gt;parentNode-&gt;removeChild($xml-&gt;childNodes-&gt;item(0));
  </code></pre>
  
  <p>Always deletes the first node:</p>
  
  <pre><code>$xml-&gt;childNodes-&gt;item(0)-&gt;parentNode-&gt;removeChild($xml-&gt;childNodes-&gt;item(1));
  </code></pre>
  
  <p>Deletes the second node on 50% of clicks:</p>
  
  <pre><code>$xml-&gt;childNodes-&gt;item(0)-&gt;parentNode-&gt;removeChild($xml-&gt;childNodes-&gt;item(2));
  </code></pre>
  
  <p>Always deletes the second node:</p>
  
  <pre><code>$xml-&gt;childNodes-&gt;item(0)-&gt;parentNode-&gt;removeChild($xml-&gt;childNodes-&gt;item(3));
  </code></pre>
  
  <p>Deletes the third node on 50% of clicks:</p>
  
  <pre><code>$xml-&gt;childNodes-&gt;item(0)-&gt;parentNode-&gt;removeChild($xml-&gt;childNodes-&gt;item(4));
  </code></pre>
  
  <p>Always deletes the third node:</p>
  
  <pre><code>$xml-&gt;childNodes-&gt;item(0)-&gt;parentNode-&gt;removeChild($xml-&gt;childNodes-&gt;item(5));
  </code></pre>
  
  <p><em><strong>ADDED MY AJAX CODE</em></strong></p>
  
  <pre><code>$('#generatedTable a.delete').live('click', function (e) {
      e.preventDefault();
  
  //TABLE ROW ID TO BE DELETED. CAN ALERT THIS FINE.
      var trID = $(this).closest('tr').attr('id'); 
  
      $.ajax({
          url: "functions/xmlDelete.php", 
          type: "POST",
          dataType: "json",
          data: "rowNumber="+ trID + "&amp;fileName=" + fileName,
          success: function(data) {
  
              $.ajax({
                  url: "functions/xmlParser.php", 
                  type: "POST",
                  dataType: "json",
                  data: "fileName="+ fileName,
                  success: function(data) {
  
                      $('#xmlTable').html(data.table);
                      $('#xmlTable').fadeIn('fast');
  
                      oTable = $('#generatedTable').dataTable({
                          "bJQueryUI": true,
                          "bPaginate": false,
                          "bLengthChange": false,
                          "bFilter": false,
                          "bSort": false,
                          "bInfo": true
                      }); 
  
                  }      
              }); 
  
          }      
      });         
  } );
  </code></pre>
  
  <p>This is what my table rows look like and I can alert the trID without problem.</p>
  
  <pre><code>&lt;tr id="0" class="odd"&gt;
    &lt;td id="0"&gt;1999 Grammy Nominees&lt;/td&gt;
    &lt;td id="1"&gt;Many&lt;/td&gt;
    &lt;td id="2"&gt;USA&lt;/td&gt;
    &lt;td id="3"&gt;Grammy&lt;/td&gt;
    &lt;td id="4"&gt;10.20&lt;/td&gt;
    &lt;td id="5"&gt;1999&lt;/td&gt;
    &lt;td align="center"&gt;&lt;a class="edit" href=""&gt;Edit&lt;/a&gt;&lt;/td&gt;
    &lt;td align="center"&gt;&lt;a class="delete" href=""&gt;Delete&lt;/a&gt;&lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>Can anyone help explain what I'm seeing here. Thanks!</p>