Discussions

XML & Web services: JDOM element deletion from ElementList problem

  1. JDOM element deletion from ElementList problem


    Hi,

    We have one XML file with multiple table elements like this. We are using Jdom for xml changes.
    ------------------------------------------------------------------

    <table id="os104" name="OtherSubmitter" />
    <table id="op105" name="OtherPersonnel" />
    <table id="s107_111" name="xxx">
        <row id="1" status="new" modifiedby="sub" modifiedon="08-11-2004" versionno="1">
    <value id="_107_3">ffSheep</value>
    <value id="_107_1">fffSheep Sheep</value>
    <value id="_107_5">ffffBoswell</value>
    <value id="_107_6">1156</value>
    <value id="_107_4">02</value>
    <value id="_107_2">Sheep</value>
       </row>
    </table>
    <table id="rb108_111" name="xx Agents " parentId="1" />
    <table id="rb108_112" name="xxIsotopes " parentId="1" />
    <table id="rb108_113" name="xx Devices " parentId="1" />
    <table id="rb108_114" name="xx Equipment " parentId="1" />
    <table id="rb108_115" name="xxx table " parentId="1" />
    <table id="rb108_116" name="xxx Agent " parentId="1" />
    --------------------------------------------------------------------
    We want to delete the "<table" elements which are having parentId="1".

    For deleting we are using the following code:

    for (int i = 0; i < tableList.size(); i++) {
     Element tableElement = (Element) tableList.get(i);
     if(tableElement.getAttributeValue("parentId")!=null){
           tableList.remove(i);
       }
    }

    tableList contains List of table Elementvalues.

    But, I am getting the following result:
    ------------------------------------------------------------------

    <table id="os104" name="OtherSubmitter" />
    <table id="op105" name="OtherPersonnel" />
    <table id="s107_111" name="xxx">
        <row id="1" status="new" modifiedby="sub" modifiedon="08-11-2004" versionno="1">
    <value id="_107_3">ffSheep</value>
    <value id="_107_1">fffSheep Sheep</value>
    <value id="_107_5">ffffBoswell</value>
    <value id="_107_6">1156</value>
    <value id="_107_4">02</value>
    <value id="_107_2">Sheep</value>
       </row>
    </table>
    <table id="rb108_115" name="xxx table " parentId="1" />
    <table id="rb108_116" name="xxx Agent " parentId="1" />
    --------------------------------------------------------------------

    What could be the mistake in my code.
    Please help me .

    Thanks,
    LALITH
  2. I think the problem is not with your XML stuff it the loop your are iterationg over a container is causing the problem. In your loop, you are removing elements from the container, which in effect will shrink the size and hence will create possibilities to miss some locations.

    Idiom: Do not remove items from a dynamic container while iterating over the same.

    Solution: Use "Iterators" instead of the for loop.

    Here is an example:

    public class IteratorTester {
    public static void main(String args[]) {
     String[] num = {"one", "two", "three", "four", "five",
                   "six", "seven", "eight", "nine", "ten"};
     Vector aVector = new Vector(java.util.Arrays.asList(num));
     System.out.println("Before Vector: " + aVector);
     Iterator nums = aVector.iterator();
     while(nums.hasNext()) {

     String aString = (String)nums.next();
     System.out.println(aString);
     if (aString.length() > 4)
      nums.remove();
     }
     System.out.println("After Vector: " + aVector);
     }
    }


    cheerz
    -vaheesan selvarajah
    http://vahees.blogspot.com
  3. Thanks for your valuable suggestion.

    LALITH
  4. Also you might use XPath to filter out all of your elements and create a container detached list of items do delete:

    import org.jdom.xpath.XPath; //do not forget this

    .
    .
    .

    List nodesToDelete = XPath.selectNodes(root, "//table[@parentId='1']");

    //Let's say root = Document.getRootElement()

    int size = nodesToDelete.size();

    for (int i=0; i<size; i++){

       ((Element)nodesToDelete.get(i)).detach();
    }
  5. Thank you very much. I changed my code by using this.

    Thanks,
    LALITH