[jdom-interest] Proper way to prune a tree with removeContent

Phill_Perryman at Mitel.COM Phill_Perryman at Mitel.COM
Wed Nov 9 08:28:25 PST 2005


Bet it was a concurrent modification exception.

If you iterate over a list of three elements and remove the second one
while iterating over it the indexes will get messed up by the ime you get
to the third (in a list which now only has two?)

I am sure I saw a previous article about iterating over the list from size
back to zero so you did not try to access elements that have been removed.

/Phill
IS Dept, Software Engineer.
phill_perryman at mitel.com
http://www.mitel.com
Tel: +44 1291 436023


                                                                           
             "Jeff Rosler"                                                 
             <Jeff.Rosler at flat                                             
             ironssolutions.co                                          To 
             m>                        <jdom-interest at jdom.org>            
             Sent by:                                                   cc 
             jdom-interest-bou                                             
             nces at jdom.org                                         Subject 
                                       [jdom-interest] Proper way to prune 
                                       a tree with removeContent           
             09/11/2005 16:00                                              
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




Hi All,

I'm running JDOM 1.0 and I just ran into an interesting corruption problem.
In my code, I'm parsing XML elements in a doc and removing specific
elements (branches) based on their attribute values. Please see the code
snippet below.

I initially call this code with profileDOM(null,doc.getRootElement()). It
recursively parses the tree until my checkAttributes() method returns
false. At this point, it removes that element. If it doesn't return false
then it gets the list of children and calls itself (recursion) for each
child.

The problem occurs in the removeContent call. The removeContent correctly
removes the content, but appears to screw up the list. The second time that
removeContent is called with a different parent, it removes the wrong
content node. It appears that the internal list somehow gets screwed up.
While I was debugging this in my IDE, I took a look at the content list in
the parent element and the parent.indexOf(node) was returning the wrong
index for the actual content. In my sample, it returned a text node instead
of an element!

So, two questions ?
If this isn't the correct type of code to prune content, what is? I
remember trying an iterator a while back, but ended up getting some
exception.
The second is, isn't this a bug? Is this the right forum to report it?

    public void profileDOM(Element parent, Element node)  throws Exception
    {
        if (parent != null && !checkAttributes(node))
        {
            // previously just doing parent.removeContent(node);
            int index = parent.indexOf(node);
            Content content = parent.removeContent(index);
            if (content == null)
            {
                logger.info("Removal of element <" + node.getName() + ">
failed!");
            }
        }
        else
        {
                List list = node.getChildren();
                for(int i=0;i<list.size();i++)
                {
                    Element child = (Element)list.get(i);

                    // check to see if we should delete child
                    profileDOM(node,child);
                }
        }
    }

Thanks,
Jeff Rosler
Consultant
Flatirons Solutions Corporation
XML and Content Management Solutions
2555 55th Street
Suite 100D
Boulder, CO  80301
303-544-0514 x111
jeff.rosler at flatironssolutions.com
"The man with a new idea is a crank until the idea succeeds"
Mark Twain
 _______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com






More information about the jdom-interest mailing list