title: remove node from XML file java program
body: <p>When I try to remove a node from XML node from java program it is giving me a strange problem. It is removing alternate nodes. I have to remove existing nodes before inserting new nodes.
  my xml file is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="windows-1252" ?&gt;
  &lt;chart&gt;
  &lt;categories&gt;
   &lt;category label="3 seconds"/&gt;
   &lt;category label="6 seconds"/&gt;
   &lt;category label="9 seconds"/&gt;
   &lt;category label="12 seconds"/&gt;
  &lt;/categories&gt;
  
  &lt;/chart&gt;
  </code></pre>
  
  <p>my java program is:</p>
  
  <pre><code>      DocumentBuilderFactory  docFactory  = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  
        Document doc = docBuilder.parse(filePath);
  
  
        Node categories = doc.getElementsByTagName("categories").item(0);
  
        NodeList categorieslist = categories.getChildNodes();
  
        // if exists delete old data the insert new data.
  
  
        for (int c = 0; c &lt; categorieslist.getLength(); c++) {
  
          Node node = categorieslist.item(c);
          categories.removeChild(node);
        }
      for(int i=1;i&lt;20;i++){
  
      Element category = doc.createElement("category");
      category.setAttribute("label",3*i+" seconds");
      categories.appendChild(category);
    }
  </code></pre>
  
  <p>This code is deleting alternative nodes I don't know why. The resulting XML is showing like this:</p>
  
  <pre><code>&lt;categories&gt;
   &lt;category label="6 seconds"/&gt;
   &lt;category label="12 seconds"/&gt;
   &lt;category label="3 seconds"/&gt;
   &lt;category label="6 seconds"/&gt;
   &lt;category label="9 seconds"/&gt;
        .....
        .....
   &lt;/categories&gt;
  </code></pre>