remove node from XML file java program
===

when i try to remove node from XML node from java program it is giving strange problem. It is removing alternative nodes. Actually i have to remove existing nodes before inserting new nodes.
my xml file is

    <?xml version="1.0" encoding="windows-1252" ?>
    <chart>
    <categories>
     <category label="3 seconds"/>
     <category label="6 seconds"/>
     <category label="9 seconds"/>
     <category label="12 seconds"/
    </categories>
    
    </chart>

my java program is

          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 < categorieslist.getLength(); c++) {
            
            Node node = categorieslist.item(c);
            categories.removeChild(node);
          }
        for(int i=1;i<20;i++){
        
        Element category = doc.createElement("category");
        category.setAttribute("label",3*i+" seconds");
        categories.appendChild(category);
      }
This code is deleting alternative nodes i don't know why. the result xml is showing like this
  

    <categories>
     <category label="6 seconds"/>
     <category label="12 seconds"/>
     <category label="3 seconds"/>
     <category label="6 seconds"/>
     <category label="9 seconds"/>
          .....
          .....
     </categories>
    