Synopsis:
Does anyone have an example of how to catch DOM mutation events? Is there a
way to do it using only the JAXP and DOM interfaces?
Version: Xerces 1.2.3
SDK: Sun JDK 1.3 on Windows 2000 and IBM JDK 1.3 on RedHat Linux 7.1
I'm trying to use the DOM Level 2 mutation events to find out if my document
has been changed. I just can't seem to get it to work. Here is what I have
done:
Create an implementation of EventListener
public class MyEventListener implements EventListener
{
public MyEventListener()
{
// empty
}
public void handleEvent(Event event)
{
System.out.println("Caught event " + event.getType());
}
}
Get the DOMImplementation and query for the feature "MutationEvents"
DOMImplementation domImplementation =
DOMImplementationImpl.getDOMImplementation();
boolean hasFeature = domImplementation.hasFeature("MutationEvents",
"2.0");
System.out.println("Has feature MutationEvents = " + hasFeature);
This returns 'true'.
Cast my Document to a DocumentImpl and query for the property
"MutationEvents"
DocumentImpl documentImpl = (DocumentImpl) document;
System.out.println("Mutation events = " +
documentImpl.getMutationEvents());
documentImpl.setMutationEvents(true);
System.out.println("Mutation events = " +
documentImpl.getMutationEvents());
This initially returns 'false', but returns 'true' after I update the
property.
I then created a MutationEvent to find out what the event type might be.
Event event = documentImpl.createEvent("MutationEvent");
System.out.println("Mutation event type = " + event.getType());
I initially used the type "MutationEvents", but that throws a DOMException :
NOT_SUPPORTED_ERR, so I used "MutationEvent" and that seemed to work. This
returns 'null'.
Now I add my event listener to the document.
EventTarget eventTarget = (EventTarget) docSource.toDocument();
EventListener eventListener = new MyEventListener();
eventTarget.addEventListener("MutationEvent", eventListener, false);
eventTarget.addEventListener("MutationEvent", eventListener, true);
eventTarget.addEventListener(null, eventListener, false);
eventTarget.addEventListener(null, eventListener, true);
I add this as a bubbling listener and as a capturing listener with type
"MutationEvent", "MutationEvents" and null. Then I change my document by
adding an attribute to an element in my document. My event handler never
gets called.
Does anyone have an example of how to catch DOM mutation events? Is there a
way to do it using only the JAXP and DOM interfaces?
Thanks for your help.
David Blankenship
KLA-Tencor Control Solutions
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]