admin 管理员组

文章数量: 887029

dom4j

今天在读取xml文件进行排序时,报了一个很奇怪的问题,把处理结果记录如下:

InputStream in = new FileInputStream("D:/news_02120101_0212010102.xml");
Reader reader = new InputStreamReader(in, "utf-8");
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(reader);

Element tempE = (Element) document.getRootElement().elements("channel").get(0); // item +
List<Element> list = tempE.elements("item");

Collections.sort(list, new ComparatorElement());


错误信息:
org.dom4j.IllegalAddException: The node "org.dom4j.tree.DefaultElement@da2cef [Element: <item attributes: []/>]" could not be added to the element "channel" because: The Node already has an existing parent of "channel"
at org.dom4j.tree.AbstractElement.addNode(AbstractElement.java:1533)
at org.dom4j.tree.BackedList.set(BackedList.java:92)
at java.util.AbstractList$ListItr.set(AbstractList.java:412)
at java.util.Collections.sort(Collections.java:163)
at cn.wasu.ftp.main.WinMain.main(WinMain.java:87)

原因:List<Element> list = tempE.elements("item");
这里得到的list是BackedList,这个list不允许存在重复元素,而Collections.sort方法在拷贝的时候,会使得list里面存在重复的元素,所以就报错了。

解决办法:自己new ArrayList,然后把list的节点拷贝到新的list里面。


备注1:
Collections.sort源码:
public static <T> void sort(List<T> list, Comparator<? super T> c) {
Object[] a = list.toArray();
Arrays.sort(a, (Comparator)c);
ListIterator i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set(a[j]);
}
}

参考地址:
/

This is not a bug: the Collections.sort() algorithm requires
the List to be fully modifiable, which is not entirely the
case with the BackedList because it does not allow duplicate
items. To sort the List, another algorithm should be used
which doesn't require that the list should allow duplicate
entries.

本文标签: DOM4j