Java + DOM: Registering and using modification listeners: tutorials?

Please give me some tutorials or other explanatory examples of how to register and use change listeners with the Java DOM implementation.

On the Internet, I only find Javascript or Flex examples.

My goal is to find out when Node was changed.

I tried several approaches, nothing works. Maybe the Java DOM does not support this feature?

+4
source share
1 answer

Got it!

Casting was a trick!

I was looking for implementations of org.w3.dom.events.EventTarget , but it seems that only inner classes seem to use it. Therefore, you just need to drop it manually (just assuming Node instanceof EventTarget ).

 org.w3c.dom.events.EventListener myModificationListener = new org.w3c.dom.events.EventListener() { @Override public void handleEvent(Event e) { if (e instanceof MutationEvent) { MutationEvent me = (MutationEvent) e; System.out.println("type: " + me.getType() + ", dest: " + me.getTarget()); } } }; Node someDomNode = ... // here the unusual casting magic happens ((EventTarget) node).addEventListener( "DOMSubtreeModified", // constant myModificationListener, true); // modify the node here by appending a child // -> listener gets invoked 
+7
source

Source: https://habr.com/ru/post/1304696/


All Articles