javaxmlremovechildjdomjdom-2

Can't remove element and child element from an XML document by Java code


This is my xml document:

<definitions>       
<task name="TASK1"
     class="CLASS"
     group="GROUP">
  <trigger count="3" interval="400"/>
  <property xmlns:task="URI"
            name="PROPERTY2"
            value="VALUE1"/>
  <property xmlns:task="URI"
            name="PROPERTY2"
            value="VALUE2"/>
</task>
<task name="TASK1"
     class="CLASS"
     group="GROUP">
  <trigger count="1" interval="600"/>
  <property xmlns:task="URI"
            name="PROPERTY2"
            value="VALUE1"/>
  <property xmlns:task="URI"
            name="PROPERTY2"
            value="VALUE2"/>
</task>
<another_tag name="a_name"/>
<another_tag2 name="a_name2"/>
<another_tag3>  something in the middle </another_tag3>

</definitions>

I have to delete all <task> tags and what inside them. I used this java code:

Document esb = new Document();

    SAXBuilder saxBuilder = new SAXBuilder();


    try {
       esb = saxBuilder.build(new File("C:\\...path\\file.xml")); 
    }
    catch (JDOMException ex) {
       System.err.println(ex);
    }
    catch (IOException ex) {
       System.err.println(ex);
    }
    Element root = esb.getRootElement();
    boolean b = root.removeChild("task");
    System.out.println(b);

I can't understand how to obtain an xml file without <task> tag and containing only <another_tag> tag. I've looked for other solutions but nothing useful. I also used removeContent() method, but nothing. I imported jdom2 libraries, I need to use recent libraries, because there are bad interactions among jdom and jdom2, so I would prefer to use recent libraries only. Any suggest about how to remove some element from this xml CODE?


Solution

  • I solved this problem by managing namespace. There is the code:`

    Document doc = new Document();
    
    SAXBuilder saxBuilder = new SAXBuilder();
    try {
           doc = saxBuilder.build(new File("....path\MyXMLfile.xml")); 
        }
        catch (JDOMException ex) {
           System.err.println(ex);
        }
        catch (IOException ex) {
           System.err.println(ex);
        }
    
    
        Element root = esb.getRootElement();
        System.out.println(root.getName());  // it prints "definitions"
    
        Namespace namespace = Namespace.getNamespace("task","http://....myDefinitionsNamespace....");
    
        boolean b = root.removeChildren("task", namespace);
    
        System.out.println(b);
    
        XMLOutputter xmlOutputter = new XMLOutputter();
        xmlOutputter.setFormat(Format.getPrettyFormat());
        System.out.println(xmlOutputter.outputString(doc)); //so we can see new XML FILE
    

    In order to understand this code, we need starting xml too:

    <?xml version="1.0" encoding="UTF-8"?>
    <definitions xmlns="http://....myDefinitionsNamespace....">
    <task name="MyTask"
         class="....myClass...."
         group="....myGroup...."> 
    <taskChild/>
    </task>
    <anotherTag1/>
    <anotherTag2>
    <task/>
    .
    .
    .
    </definitions>
    

    The result is an XML file without every task tag, it will contain anotherTags only. Then you need to define the output (for example into a file with a FileOutputStream instance). Thanks to @OkieOth and @rolfl.