title: How to remove element from xml file using text content in java?
body: <p>i have the following in xml file      </p>
  
  <pre><code>&lt;data&gt;
    &lt;group&gt;
      &lt;groupname&gt;a&lt;/groupname&gt;
      &lt;groupuser&gt;Saw&lt;/groupuser&gt;
      &lt;groupuser&gt;John&lt;/groupuser&gt;
    &lt;/group&gt;   
    &lt;group&gt;    
      &lt;groupname&gt;b&lt;/groupname&gt;    
      &lt;groupuser&gt;John&lt;/groupuser&gt;    
      &lt;groupuser&gt;Saw&lt;/groupuser&gt;    
    &lt;/group&gt;   
    &lt;group&gt;    
      &lt;groupname&gt;c&lt;/groupname&gt;    
      &lt;groupuser&gt;John&lt;/groupuser&gt;    
      &lt;groupuser&gt;Saw&lt;/groupuser&gt;    
    &lt;/group&gt;   
    &lt;user&gt;   
      &lt;username&gt;John&lt;/username&gt;    
      &lt;password&gt;1234&lt;/password&gt;    
    &lt;/user&gt;    
  &lt;/data&gt;
  </code></pre>
  
  <p>I am trying to remove this element</p>
  
  <pre><code>&lt;groupuser&gt;John&lt;/groupuser&gt;
  </code></pre>
  
  <p>This is my method:</p>
  
  <pre><code>public void removeUserGroup(String username) {
      try {
          File fXmlFile = new File(filePath);
          DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
          DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
          Document doc = dBuilder.parse(fXmlFile);
          doc.getDocumentElement().normalize();
  
          NodeList nList = doc.getElementsByTagName("group");
  
          for (int temp = 0; temp &lt; nList.getLength(); temp++) {
              Element group = (Element) nList.item(temp);
  
              for (int temp2 = 0; temp2 &lt; group.getElementsByTagName("groupuser").getLength(); temp2++) {
                  Element name = (Element) group.getChildNodes().item(temp2);
  
                  if (name.getTextContent().equals(username))
                      group.getParentNode().removeChild(group);
              }
          }
      } catch (Exception ex) {
          System.out.println("Database exception");
      }
  }
  </code></pre>
  
  <p>The code enters here and it doesn't throw exception</p>
  
  <pre><code>group.getParentNode().removeChild(group);
  </code></pre>
  
  <p>But nothing happens in the xml file!
  I used this method from another question here i modified it to loop on children of <code>&lt;group&gt;</code> but doesn't seem to work</p>