0

我正在使用 DOM4J 来分析两个 XML 元素。元素如下:

<element1 attr="val">text</element1> //org.dom4j.Element = e1

<element1 attr="val">OtherText</element1>  //org.dom4j.Element = e2

这两个元素都存储在org.dom4j.Element实例中,e1并且e2.

我希望这两个元素具有相同的属性,所以我希望:

e1.attributes().containsAll(e2.attributes())

返回true,但它实际上返回false

当我检查这两个属性时,我发现以下字符串表示:

org.dom4j.tree.DefaultAttribute@552c8fa8 [Attribute: name attr value "val"]

org.dom4j.tree.DefaultAttribute@26d58939 [Attribute: name attr value "val"]

我错过了一些明显的东西吗?除了编写我自己的containsAll函数来检查这种行为之外,你还能想到我应该尝试的其他方法吗?

4

1 回答 1

4

I believe you'll have to write your own containsAll(). You're seeing the default List.containsAll(), which compares items using equals(). Since DefaultAttribute doesn't override equals() to make your comparison evaluate to true, you're out of luck.

于 2011-01-10T20:11:31.453 回答