[jdom-interest] Document Listeners?

Jon Baer jonbaer at digitalanywhere.com
Mon Mar 26 10:28:56 PST 2001


That is kinda what I was thinking.  What happens in my application is I read a
Document and allow it to be read-only access from different areas of the
application.  So for example when I call:

name = doc.getRootElement().getChild("name").getText();

This value is only valid for when after the document is built (doc).  But if I change
the value in the XML file *during* the time that the application is open, Id like to
be able to get that new value without having to do anything except save the file.

The FileWatcher util class seems like it would need to interact or be part of the
Document class?  Or would I use the Document in there:

FileWatcher fw = new FileWatcher(doc);
fw.start();

Since the FileWatcher would need to keep track of *when* the Document was built.

- Jon

Patrick Dowler wrote:

> 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
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com




More information about the jdom-interest mailing list