简体   繁体   English

Java + DOM:注册和使用修改监听器:教程?

[英]Java+DOM: Registering and using modification listeners: tutorials?

Please point me to some tutorials or other explaining examples about how to register and use modification listeners with Java's DOM implementation. 请指出一些有关如何使用 Java的DOM实现注册和使用修改侦听器的教程或其他解释示例。

On the web I find only Javascript or Flex examples. 在网络上我只找到Javascript或Flex示例。

My target is to get to know when a Node was modified. 我的目标是了解Node修改时间。

I tried out several approaches, nothing works. 我尝试了几种方法,没有任何作用。 Could it be that Java's DOM doesn't support this feature? 是不是Java的DOM不支持这个功能?

Got it! 得到它了!

Casting was the trick! 铸造就是诀窍!

I was looking for implementations of org.w3.dom.events.EventTarget , but it seems that only internal classes implement it. 我正在寻找org.w3.dom.events.EventTarget实现,但似乎只有内部类实现它。 So it have just to be casted by hand (by just assuming that Node instanceof EventTarget ). 所以它只需要手工制作(通过假设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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM