3

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?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
FDC
  • 317
  • 3
  • 16

4 Answers4

1

The api says this for the function 'removeChild': ... This removes the first child element (one level deep) with the given local name and belonging to no namespace...

The removeChild function removes only one child. So if you want remove all childs with a specific name you need to use a loop. If the function call can't find a node with the desired name it returns with false.

If I work with your sample xml the third call of removeChild returns with false. So the following code will delete all task childs

...
    boolean b = root.removeChild("task");
    while (b)
        b = root.removeChild("task");
...
OkieOth
  • 3,604
  • 1
  • 19
  • 29
  • is the document root, is one of the first children... if i use System.out.println(root.getName()); it prints "definitions". Maybe I have to understand the problem about the namespace. – FDC Feb 24 '16 at 13:41
  • @FDC what is the output of System.out.println(esb.getRootElement().getName())? – OkieOth Feb 24 '16 at 14:08
  • It prints "definitions" – FDC Feb 24 '16 at 14:24
  • @FDC your code seems to be right but you have two 'task' childs in your Document you should call remove in a loop – OkieOth Feb 24 '16 at 14:42
  • Oh, I have a lot of tag in my XML :) I have to delete all of them and i thought i could reach this goal with a single method, but i can't. – FDC Feb 24 '16 at 14:53
0

In addition to what OkieOth has said, you are only removing the first task.

The other issue is that you are not writing the JDOM out again to file. You need to save over the existing file in order to see the changes.

If you want to apply the changes back to the file, consider this:

SAXBuilder saxBuilder = new SAXBuilder();

try {
    Document esb = saxBuilder.build(new File("C:\\...path\\file.xml")); 
    Element root = esb.getRootElement();
    boolean b = root.removeChild("task");
    System.out.println(b);
    XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
    try (OutputStream os = new FileOutputStream("C:\\...path\\file.xml")) {
        xout.output(os, esb)
    }
}
catch (JDOMException ex) {
   System.err.println(ex);
}
catch (IOException ex) {
   System.err.println(ex);
}
rolfl
  • 17,539
  • 7
  • 42
  • 76
  • I didn't include in my example the part of the code with XMLoutputter, but i knew it, thank you, it doesn't work anyway. the document "esb" presents "task" tags. – FDC Feb 24 '16 at 13:37
0

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.

FDC
  • 317
  • 3
  • 16
0

Here is the code to use XPath to remove all task nodes incrementally using vtd-xml

import com.ximpleware.*;
import java.io.*;

public class removeElement {
    public static void main(String s[]) throws VTDException,IOException{
        VTDGen vg = new VTDGen();
        if (!vg.parseFile("input.xml", false))
            return;
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/definition/task");
        XMLModifier xm = new XMLModifier(vn);
        int i=0;
        while((i=ap.evalXPath())!=-1){
            xm.remove();
        }
        xm.output("output.xml");

    }
}
vtd-xml-author
  • 3,319
  • 4
  • 22
  • 30