Skip to content

JDOM2 Feature Attribute is Content

Rolf edited this page Apr 1, 2012 · 3 revisions

This feature has been reverted. It did not add any significant value. The original description is here in case anyone wants to see what it was, and contribute to further debate.

The added complexity in the Content-Handling to do the Element.addContent() type functions correctly is an unnecessary overhead, and there is no real circumstance where Attribute-being-a-Content is actually useful. The original motivation to implement this was to support simpler XPath queries where all the returned data from an XPath expresion is Content, but that is still not possible because XPath can return String, Double, and Boolean anyway.

Attribute is now treated in the same way as it always has been in JDOM: it's own structure. One change coming from the Attribute-is-content concept was that Attributes had 'Namespaces In Scope'. Check out the details of the Namespaces In Scope feature because Attribute now also implements the NamespaceAware interface to compensate for losing that functionality that it inherited from Content.


Attribute is now Content (but Document is not?). This is a debatable change still.

The Attribute class does not extend Content in JDOM 1.x. The DOM and XPath libraries each treat Attributes as 'just another Node', and Attributes have a common inheritance ancestry with other XML nodes like Text, and Comment.

Since Attribute needs to follow the same parent/child relationship as other content it is useful to simply have common ancestry to deal with these relationships (and with Namespace handling).

For these reasons Attribute now extends Content. This change should be transparent to most people, but, it makes it possible to do things like simply adding Attribute instances with other Content, or in the same way as other content.

Specifically, calling myelement.addContent(new Attribute("att", "val")); is functionally equivalent to myelement.setAttribute(new Attribute("att", "val"));. Attribute instances added using Element.addContent() and related methods will not be visible in Element.getContent() and related method results, only in Element.getAttributes() and related methods.

Thus, while 'Attribute is a Content' makes the XML tree easier to manage, it also introduces some ambiguity in the Element.addContent() and related methods.

The value of this change (like all other changes) is still open for debate.

Clone this wiki locally