使用JDOM重写XML / DOM树的问题(ConcurrentModificationException)

时间:2022-01-31 20:46:09

I need to walk a JDOM tree and make changes as I go along; at this point, changes are mostly adding new elements right now but could also include reordering elements or removing elements. All work is done on the same thread so there are no concurrency issues.

我需要走一条JDOM树,随着时间的推移做出改变;此时,更改主要是添加新元素,但也可以包括重新排序元素或删除元素。所有工作都在同一个线程上完成,因此没有并发问题。

This turns out to be difficult because JDOM iterators can throw a ConcurrentModificationException if you try to add a node during traversal. From what I can see, JDOM uses lists instead of directly linking DOM nodes and this makes it difficult to do modifications on the fly.

事实证明这很困难,因为如果您尝试在遍历期间添加节点,JDOM迭代器可能会抛出ConcurrentModificationException。从我所看到的,JDOM使用列表而不是直接链接DOM节点,这使得很难动态地进行修改。

I've seen a couple of recommendations on how to deal with this, such as deferring the adds until after the traversal is done, or building a new tree on the fly so that the traversed tree remains unchanged. These won't work for me because I need a consistent view of the tree as I modify it.

我已经看到了一些关于如何处理这个问题的建议,例如在遍历完成之后推迟添加,或者动态构建新树以使遍历的树保持不变。这些对我不起作用,因为在修改树时我需要一致的树视图。

I'm beginning to suspect that JDOM just won't work here. Do any of the other Java DOM models make this easier? Or is there a way to do this in JDOM?

我开始怀疑JDOM在这里不起作用。是否有任何其他Java DOM模型使这更容易?或者有没有办法在JDOM中执行此操作?

3 个解决方案

#1


3  

I've come up with what looks like an easy solution using JDOM. Rather than using the JDOM iterator directly, I use the iterator to create a list of nodes and then traverse using this list. Since this list is not "live", my scripts can modify the tree (and see the changes) without affecting the traversal. The traversal won't see structural changes but that should not be a problem.

我想出了一个使用JDOM的简单解决方案。我不是直接使用JDOM迭代器,而是使用迭代器创建节点列表,然后使用此列表进行遍历。由于此列表不是“实时”,因此我的脚本可以修改树(并查看更改)而不会影响遍历。遍历不会看到结构变化,但这应该不是问题。

#2


0  

Is there a reason you can't simply do two passes?

有没有理由你不能简单地做两次通行证?

Most algorithms I'm familiar with won't require more than 2 traversals when decorating a tree (ideally, your algorithm should need a pass for initial decoration and perhaps a second for resolving references after the decoration).

我熟悉的大多数算法在装饰树时不需要超过2次遍历(理想情况下,您的算法应该需要通过初始装饰,可能需要一秒钟来解析装饰后的参考)。

#3


0  

Since you are open to using other models, you might consider Elliotte Rusty Harold's XOM API. It's rock solid, and won't allow you to create an invalid XML structure.

由于您可以使用其他模型,因此您可以考虑使用Elliotte Rusty Harold的XOM API。它坚如磐石,不允许您创建无效的XML结构。

#1


3  

I've come up with what looks like an easy solution using JDOM. Rather than using the JDOM iterator directly, I use the iterator to create a list of nodes and then traverse using this list. Since this list is not "live", my scripts can modify the tree (and see the changes) without affecting the traversal. The traversal won't see structural changes but that should not be a problem.

我想出了一个使用JDOM的简单解决方案。我不是直接使用JDOM迭代器,而是使用迭代器创建节点列表,然后使用此列表进行遍历。由于此列表不是“实时”,因此我的脚本可以修改树(并查看更改)而不会影响遍历。遍历不会看到结构变化,但这应该不是问题。

#2


0  

Is there a reason you can't simply do two passes?

有没有理由你不能简单地做两次通行证?

Most algorithms I'm familiar with won't require more than 2 traversals when decorating a tree (ideally, your algorithm should need a pass for initial decoration and perhaps a second for resolving references after the decoration).

我熟悉的大多数算法在装饰树时不需要超过2次遍历(理想情况下,您的算法应该需要通过初始装饰,可能需要一秒钟来解析装饰后的参考)。

#3


0  

Since you are open to using other models, you might consider Elliotte Rusty Harold's XOM API. It's rock solid, and won't allow you to create an invalid XML structure.

由于您可以使用其他模型,因此您可以考虑使用Elliotte Rusty Harold的XOM API。它坚如磐石,不允许您创建无效的XML结构。