Search code examples
javadom4j

DOM4J Element.attributes().containsAll() returns unexpected value


I am using DOM4J to do analysis of two XML elements. The elements are the following:

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

and

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

Both of these elements are stored in org.dom4j.Element instances, e1 and e2.

I expect that both of these elements have the same attributes, so I expect that:

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

returns true, but it actually returns false.

When I inspect both of these attributes, I find the following string representations:

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

and

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

Am I missing something obvious? Beyond writing my own containsAll function to inspect this behavior, can you think of anything else I should try?


Solution

  • 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.