Java + DOM:注册和使用修改监听器:教程? [英] Java+DOM: Registering and using modification listeners: tutorials?

查看:106
本文介绍了Java + DOM:注册和使用修改监听器:教程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请指出一些关于如何使用Java的DOM实现注册和使用修改监听器的教程或其他解释示例。

Please point me to some tutorials or other explaining examples about how to register and use modification listeners with Java's DOM implementation.

在web我只找到了Javascript或Flex示例。

On the web I find only Javascript or Flex examples.

我的目标是了解何时修改了 Node

My target is to get to know when a Node was modified.

我尝试了几种方法,没有任何作用。 Java的DOM不支持这个功能吗?

I tried out several approaches, nothing works. Could it be that Java's DOM doesn't support this feature?

推荐答案

获得!

Casting 是窍门!

我正在寻找 org.w3.dom的实现.events.EventTarget ,但似乎只有内部类实现它。所以它只是被手动(通过假设 EventTarget的Node instanceof / code>)。

I was looking for implementations of org.w3.dom.events.EventTarget, but it seems that only internal classes implement it. So it have just to be casted by hand (by just assuming that 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

这篇关于Java + DOM:注册和使用修改监听器:教程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆