[jdom-interest] Re: carrying user data around with an Element

James Strachan james at metastuff.com
Fri Nov 17 03:59:38 PST 2000

I'd suggest making your own Element class to do exactly what you need. This
kind of change should not be part of the standard JDOM API. Though I think
JDOM should support easy integration with custom derivations of the core
classes (Element, Attribute etc)

If you want to build a JDOM tree of your Element objects via SAX, try using
the SAXBuilder refactored code I've just sent to the list. Hopefully one of
these fine days it may even get incorporated into the CVS tree.

With a small amount of code and 3 classes you can achieve exacly what you
want. Here's the code.

public class MyElement extends Element {
    private Object userObject;

    public Object getUserObject() {
        return userObject;
    public void setUserObject(Object userObject) {
        this.userObject = userObject;

public class MySAXBuilder extends SAXBuilder {
    protected DefaultHandler createContentHandler( Document doc ) throws
IOException {
        return new MySAXHandler(doc);

public class MySAXHandler extends SAXHandler {
    protected Element createElement(String localName, Namespace
elementNamespace) {
        return new MyElement( localName, elementNamespace );
    protected Element createElement(String localName) {
        return new MyElement(localName);


James Strachan
email: james at metastuff.com
web: http://www.metastuff.com

----- Original Message -----
From: "David Churchville" <dmc at clearlight.com>
To: <jdom-interest at jdom.org>
Sent: Wednesday, November 15, 2000 8:38 PM
Subject: [jdom-interest] Re: carrying user data around with an Element

> I'd like to be able to set a "user object" on an Element (in lieu of
> subclassing, using factories, special builders, etc. - see Factories for
> element creation discussion).
> It would basically add the following to the Element class:
> public void setUserObject( Object o);
> public Object getUserObject();
> Purpose: allow custom application data (not related to parsing XML) to
> travel around with Element objects.
> Examples:
> - An XML tool could read data with JDOM, allow modifications visually
> modify the JDOM tree), and store arbitrary UI data in the Element, such as
> color or icon to use when rendering it.
> - An XML processor could use the JDOM tree, and mark which elements had
> visited already by storing a "visited" user object (a Boolean flag, for
> example)
> Comments?
> _______________________________________________
> To control your jdom-interest membership:

If you are not the addressee of this confidential e-mail and any
attachments, please delete it and inform the sender; unauthorised
redistribution or publication is prohibited. Views expressed are those of
the author and do not necessarily represent those of Citria Limited.

More information about the jdom-interest mailing list