0

i have the following in xml file

<data>
  <group>
    <groupname>a</groupname>
    <groupuser>Saw</groupuser>
    <groupuser>John</groupuser>
  </group>   
  <group>    
    <groupname>b</groupname>    
    <groupuser>John</groupuser>    
    <groupuser>Saw</groupuser>    
  </group>   
  <group>    
    <groupname>c</groupname>    
    <groupuser>John</groupuser>    
    <groupuser>Saw</groupuser>    
  </group>   
  <user>   
    <username>John</username>    
    <password>1234</password>    
  </user>    
</data>

I am trying to remove this element

<groupuser>John</groupuser>

This is my method:

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 < nList.getLength(); temp++) {
            Element group = (Element) nList.item(temp);

            for (int temp2 = 0; temp2 < 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");
    }
}

The code enters here and it doesn't throw exception

group.getParentNode().removeChild(group);

But nothing happens in the xml file! I used this method from another question here i modified it to loop on children of <group> but doesn't seem to work

ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
  • have you tried debugging the code, follow line per line it's execution and see how is behaving ? if you have tried that, it would be useful taht you post your results ... – Rafael Dec 04 '15 at 15:33
  • I don't see where you print your results to that file again... If you only change the xml in your code it won't change anything in the file ^^ – ParkerHalo Dec 04 '15 at 15:34

2 Answers2

1

After you have removed the element you'll have to save your output to some file to be able to see something:

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new DOMSource(doc), new StreamResult(new File("someFilePath")));

now you'll be able to see if you've deleted it correctly!

EDIT:

if you want to prettily print your file you'll have to add these lines before transforming:

transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

This will create some nice indentention and make your file much more readable!

ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
1

First you don't save the xml back to the file.

Second, this code is wrong although it might be working in certain circumstances:

for (int temp2 = 0; temp2 < group.getElementsByTagName("groupuser").getLength(); temp2++){

Save the result to NodeList instead and loop over the nodes in the list.

And third - you are removing full group not just groupuser.

Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43