title: How to remove a node in XML using JAVA
body: <p>The XML file I use is like this, pay attention that there are 4 'target' tag.    </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;&lt;project basedir="." default="end" name="precompile"&gt;
  &lt;property name="charset" value="utf-8"/&gt;
  
  &lt;target name="start"&gt;
  &lt;/target&gt;
  
  &lt;target depends="precompile-templates1,precompile-templates2" name="end"&gt;
      &lt;echo&gt;finish generating precompiled templates&lt;/echo&gt;
  &lt;/target&gt;
  
  
  
  &lt;target name="precompile-templates1"&gt;
        &lt;property name="outputJS1" value="jsp/jsp_2/js/templates.js"/&gt;
        &lt;property name="templatesPath1" value="jsp/jsp_2/js/tmpl"/&gt;
  
        &lt;java dir="${basedir}" failonerror="true" fork="true" jar="lib/js.jar"&gt;
          &lt;arg value="otherFiles/lib/rhino-handlebars-compiler.js"/&gt;
          &lt;arg value="--handlebars"/&gt;
          &lt;arg value="otherFiles/third-party/handlebars-v1.3.0.js"/&gt;
          &lt;arg value="--templates"/&gt;
          &lt;arg value="${templatesPath1}"/&gt;
          &lt;arg value="--output"/&gt;
          &lt;arg value="${outputJS1}"/&gt;
        &lt;/java&gt;
        &lt;echo&gt;Template Precompiled to web/js/compiled-templates.js&lt;/echo&gt;
          &lt;echo&gt; is now ready to compress....&lt;/echo&gt;
  &lt;/target&gt;
  
  &lt;target name="precompile-templates2"&gt;
          &lt;property name="outputJS2" value="jsp/jsp_3/js/templates.js"/&gt;
          &lt;property name="templatesPath2" value="jsp/jsp_3/js/tmpl"/&gt;
  
          &lt;java dir="${basedir}" failonerror="true" fork="true" jar="lib/js.jar"&gt;
          &lt;arg value="otherFiles/lib/rhino-handlebars-compiler.js"/&gt;
          &lt;arg value="--handlebars"/&gt;
          &lt;arg value="otherFiles/third-party/handlebars-v1.3.0.js"/&gt;
          &lt;arg value="--templates"/&gt;
          &lt;arg value="${templatesPath2}"/&gt;
          &lt;arg value="--output"/&gt;
          &lt;arg value="${outputJS2}"/&gt;
        &lt;/java&gt;
        &lt;echo&gt;Template Precompiled to web/js/compiled-templates.js&lt;/echo&gt;
          &lt;echo&gt; is now ready to compress....&lt;/echo&gt;
  &lt;/target&gt;&lt;/project&gt;
  </code></pre>
  
  <p>I want to keep the first 2 'target' node but remove the rest 2 nodes of 'target'. What should I do? I was trying to do something like this:</p>
  
  <pre><code>File fXmlFile = new File(subBuildFile);
  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder  dBuilder = dbFactory.newDocumentBuilder();
  Document  doc = dBuilder.parse(fXmlFile);
  
  Node s = doc.getElementsByTagName("target").item(1);
  
  for(int i =2; i&lt;doc.getElementsByTagName("target").getLength();i++){
      doc.getElementsByTagName("target").item(i).removeChild(doc.getElementsByTagName("target").item(i));
  }
  </code></pre>
  
  <p>But there always is a similar error like this:</p>
  
  <pre><code>Exception in thread "main" org.w3c.dom.DOMException: NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist.
  at com.sun.org.apache.xerces.internal.dom.ParentNode.internalRemoveChild(Unknown Source)
  at com.sun.org.apache.xerces.internal.dom.ParentNode.removeChild(Unknown Source)
  at xmlReader.Test.resetXML(Test.java:52)
  at xmlReader.Test.main(Test.java:39)
  </code></pre>
  
  <p>Any suggestion?</p>
  
  <p>Plus:
  Something weird when I use the following codes:</p>
  
  <pre><code>System.out.println("the number of target tag is "+doc.getElementsByTagName("target").getLength());
  
      for(int i =2; i&lt;targets.getLength();i++){
          root.removeChild(targets.item(2));
          System.out.println(i);
  
  
      }
  
      System.out.println(doc.getElementsByTagName("target").getLength());
  </code></pre>
  
  <p>The XML I use is like this:
      
       </p>
  
  <pre><code> &lt;target name="start"&gt;
   &lt;/target&gt;
  
  &lt;target depends="precompile-templates1,precompile-templates2" name="end"&gt;
      &lt;echo&gt;finish generating precompiled templates&lt;/echo&gt;
  &lt;/target&gt;
  
  
  
  
  
  
  
  
  
  
  &lt;target id="2"&gt;
  &lt;/target&gt;
  &lt;target id="3"&gt;
  &lt;/target&gt;
  &lt;target id="4"&gt;
  &lt;/target&gt;
  
  &lt;target id="5"&gt;
  &lt;/target&gt;
  &lt;target id="6"&gt;
  &lt;/target&gt;
  
  &lt;target id="7"&gt;
  &lt;/target&gt;
  &lt;target id="8"&gt;
  &lt;/target&gt;
  &lt;target id="9"&gt;
  &lt;/target&gt;
  &lt;target id="10"&gt;
  &lt;/target&gt;
  &lt;target id="11"&gt;
  &lt;/target&gt;
  &lt;target id="12"&gt;
  &lt;/target&gt;
  &lt;target id="13"&gt;
  &lt;/target&gt;
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  &lt;/project&gt;
  </code></pre>
  
  <p>The output will like this:</p>
  
  <pre><code>the number of target tag is 8
  2
  3
  4
  5
  </code></pre>
  
  <p>And the final output XML is like this:
       
       
       </p>
  
  <pre><code>&lt;target name="start"&gt;
  &lt;/target&gt;
  
  &lt;target depends="precompile-templates1,precompile-templates2" name="end"&gt;
      &lt;echo&gt;finish generating precompiled templates&lt;/echo&gt;
  &lt;/target&gt;
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  &lt;target id="11"&gt;
  &lt;/target&gt;
  &lt;target id="12"&gt;
  &lt;/target&gt;
  &lt;target id="13"&gt;
  &lt;/target&gt;
  &lt;/project&gt;
  </code></pre>