remove node from XML file java program -


When I try to remove a node from the Java node from the Java program, it is giving me a strange problem. Removing optional nodes I need to remove existing nodes before inserting new nodes. My XML file is:

  & lt ;? Xml version = "1.0" encoding = "window-1252"? & Gt; & Lt; Chart & gt; & Lt; Categories & gt; & Lt; Category label = "3 seconds" /> & Lt; Category label = "6 seconds" /> & Lt; Category label = "9 seconds" /> & Lt; Category label = "12 seconds" / & gt; & Lt; / Categories & gt; & Lt; / Chart & gt;   

My Java program is:

  DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance (); DocumentBuilder Debuilder = docFactory.newDocumentBuilder (); Document doctor = docbuilder.parse (filePath); Node categories = doc.getElementsByTagName ("Categories"). Item (0); Nodelite categories = categories.getbildode (); // If exists, delete the old data (insert int c = 0; c   

This code is removing the optional nodes, I do not know why the resulting XML is showing:

   & Lt; Category label = "12 seconds" / & gt; & Lt; Category label = "3 seconds" /> & Lt; Category label = "6 seconds" /> & Lt; Category label = "9 seconds" /> ..... ..... & lt; / Categories & gt;    

Each time you delete a child, the list shrinks, The list is not static store, so every time you call getLength (), you get the actual size

  node categories = doc.getElementsByTagName ("categories"). Item (0); Nodelite categories = categories.getbildode (); While (categorieslist.getLength ()> 0) {node node = categorieslist.item (0); Node.getParentNode () removeChild (node). }    

Comments