[jdom-interest] Document Listeners?

Patrick Dowler patrick.dowler at nrc.ca
Mon Mar 26 09:46:57 PST 2001

On 23 March 2001 23:15, Jon Baer wrote:
> Hi,
> Is there any reasonable way to add a listener to a JDOM Document to
> listen for XML doc changes and automatically rebuild itself?  Has anyone
> done this?
> Document doc = builder.build(file);
> doc.addDocumentListener(this);
> public void documentChanged(DocumentEvent e) {
> try {
>     builder.build(file);
>     } catch (Exception e) {}
> }
> Im interested in using a "live" XML file where configuration changes are
> automatically ready and immediatley available.

I'm not sure if what you want is to be notified when the in-memory
Document is altered or when the file is altered. Since you go one to
call build, I assume you arw watching the file... in which case this can be
done and has nothing to do with Document per se... it is the file that
is changing, not the Document.

What you need is something that watches the file and fires an event. 
Having said that, I think it is tough to do it without polling since it looks
like you only have File.lastModified() to work with :-(

the you'd have:

FileWatcher watcher = new FileWatcher( file );
watcher.addListener( this );
doc = builder.build(file);

public void documentChanged(DocumentEvent e) {
try {
    doc = builder.build(file); // NOTE change here
    } catch (Exception e) {}

Patrick Dowler
Canadian Astronomy Data Centre
National Research Council
Victoria, BC

More information about the jdom-interest mailing list