• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

DOM Event Listener

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking through the documentation of DOM, it is not clear to me what exacly I should do in order to capture events from a DOM Document. Whenever a change occur in the DOM Document is there always an event fired (MutationEvent etc)? I presume so, and if so, I would like to capture these events with a listener and handle them accordingly... Can anyone help out or at leasty point me in the right direction?

Thanks in advance!
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emm.. Johannes, is using DOM a requirement? If not, you can use SAX instead. It is an event-oriented API and may fit your needs better.
 
Johannes Hartman
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mapraputa... thanks for the reply! Unfortunately the documents requires extensive transformations and having the SAX would performance-speaking not be a good solution (or am I missing something about SAX?). Another plus of having to use DOM is that technologies like XPath, XPointer, XInclude, XACL etc could all be "plugged-in" without too many work-arounds.
As far as I can see I probably have to go code the actual listener interface with some events but do not know how to proceed exactly. Any suggestions or direction!
Thanks!
 
Mapraputa Is
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, then maybe you can use SAX to build DOM tree - this is often use for SAX. I do not not if will be a solution - depends on what events you want to capture. Let's see, if others suggest something better...
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are three main interfaces used with DOMII events -
EventTarget - Node objects implement the EventTarget interface. The EventTarget interface contains three methods -

  • addEventListener(String type, EventListener listener, boolean useCapture ) - this method registers classes to handle events.
  • removeEventListener(String type, EventListener listener, boolean useCapture ) - this method removes previously registered listeners.
  • dispatchEvent( Event ) - generates an event.

  • EventListener - Classes that want to consume events on a given Node or Document do so by implementing the EventListener interface. This interface has a single method - handleEvent(Event), which is called when an event occurs.
    Event - This class itself is the currency that EventListeners and EventTargets trade in. Event objects contain a number of fields that the consumer can act on, such as the Node associated with the event - whether the event is cancelable or not, whether the EventListener has to change how the event is processed etc. For example, the stopPropagation() method allwos a listener to stop a bubbling event from being delivered to listeners registered higher up the DOM tree.
    There are a number of additional classes that derive from the Event class -

    • UIEvent - Events that encapsulate additional information about the View in which they occured.
    • MouseEvent - Events that contain pointer-related information, such as screen x/y positions, the keyboard state, whether any special keys ( CTRL, ALT etc ) were pressed and so on.
    • MutationEvent - Events that contain related Node information such as previous and current value etc. MutationEvents have a number of derived events such as DOMSubtreeModified, DOMNodeInserted, DOMNodeRemoved, DOMAttrModified and so on.


    • To answer your question - adding and handling DOM events is no different than AWT event mechanisms. Simply implement the event listener, register it and have fun!
      Good luck!
      ------------------
      Ajith Kallambella M.
      Sun Certified Programmer for the Java�2 Platform.
      IBM Certified Developer - XML and Related Technologies, V1.
      [This message has been edited by Ajith Kallambella (edited May 09, 2001).]
 
Johannes Hartman
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ajith - Thanks a million for your reply!! It points me in the right direction... Could you maybe refer me to some code or examples where I could take a closer look at how exactly I should do this - it will at least restrain me from running to this forum for every little question in order to seek the answer!
Thanks again!
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johannes,
Since DOM II spec is very new, I am not sure if there is any parser support available for new event model today. You will have to wait for a few more months....
 
Johannes Hartman
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ajith
I have made some attempts to use the event model for the DOM, but unfortunately I must miss something somewhere...maybe you could help!
In my attempts I have used the xerces package. I implemented a listener class which implements the EventListener interface and the handleEvent method. Then, added the listener to the DocumentImpl object as follows:
doc.addEventListener("DOM_SUBTREE_MODIFIED", new DOMDocumentListener(), true);
doc.addEventListener("DOM_SUBTREE_MODIFIED", new MyDOMDocumentMutationListener(), false);
Also, I set setMutationEvents(true) so it will generate mutation events.
However, when I remove nodes from the tree, I do not receive any notifications - the code in the handleEvent method is not called.
What seems to be the problem here? Any suggestions?
Thanks in advance for your help!
Johannes
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The sun DOM tutorial has an example of an event driven DOM. Refer to the DOM section.
HTH
 
Johannes Hartman
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sanjay Bahal:
The sun DOM tutorial has an example of an event driven DOM. Refer to the DOM section.
HTH


Sanjay - I searched for the event driven DOM tutorial/example you mentioned but was unable to locate it. Could you please send me a link to this "event driven" example for DOM - really appreciate it!
Thanks!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!