title: Removing element node from xml file based on having specific attribute using DOM
body: <p>I have the following xml file structure </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;data Calls="1" Method="setDescription(java.lang.String) void" Class="Symptom" Package="emrservicedesign" Base_Time_.seconds.="0.000004"/&gt;
  &lt;data Invoked_by="1" Method="testSetDescription() void" Class="SymptomTest"/&gt;
  &lt;data Calls="4" Method="setDescription(java.lang.String) void" Class="Symptom" Package="emrservicedesign" Base_Time_.seconds.="0.000004"/&gt;
  &lt;/View&gt; 
  </code></pre>
  
  <p>what am trying to do is to remove all element nodes that have "Invoked_by" as attribute, so the the result of the above xml file should be </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;data Calls="1" Method="setDescription(java.lang.String) void" Class="Symptom" Package="emrservicedesign" Base_Time_.seconds.="0.000004"/&gt;
  &lt;data Calls="4" Method="setDescription(java.lang.String) void" Class="Symptom" Package="emrservicedesign" Base_Time_.seconds.="0.000004"/&gt; 
  &lt;/View&gt;
  </code></pre>
  
  <p>This is the code I have written </p>
  
  <pre><code>NodeList nodes = doc.getElementsByTagName("data");     
  for (int i = 1; i &lt; nodes.getLength(); i++) 
  {Node node = nodes.item(i); 
  Element e = (Element) node;
  if (e.hasAttribute("Invoked_by")){ nodes.item(i).removeChild(node);}
  </code></pre>
  
  <p><strong>This is the error</strong> </p>
  
  <pre><code>org.w3c.dom.DOMException: NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist.
  </code></pre>
  
  <p>I appreciate if Any body can help</p>