DOM level 2 Events : DOM : XML : Java examples (example source code) Organized by topic

Java
C++
PHP
Java Home »  XML   » [  DOM  ]  Screenshots 
 



DOM level 2 Events




/*
  Java, XML, and Web Services Bible
  Mike Jasnowski
  ISBN: 0-7645-4847-6
*/

import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.SAXException;
import org.w3c.dom.*;
import org.apache.xerces.dom.*;
import java.io.IOException;
import org.w3c.dom.events.*;
import org.apache.xerces.dom.events.*;

public class DemoEventsModule implements EventListener {

  public static void main(String args[]) throws SAXException, IOException {

    new DemoEventsModule();
  }

  public DemoEventsModule() throws SAXException, IOException {

    DOMParser parser = new DOMParser();

    parser.setFeature(
        "http://apache.org/xml/features/dom/defer-node-expansion",
        false);
    parser.parse("games.xml");

    Document doc = parser.getDocument();

    //((DocumentImpl)doc).setMutationEvents(true);

    Node node = doc.getDocumentElement().getFirstChild();
    Node clone = node.cloneNode(true);

    ((DocumentImpldoc).addEventListener("DOMNodeRemoved", this, false);
    EventTarget evTarget = (EventTargetdoc;
    evTarget.addEventListener("DOMNodeInserted", this, false);

    // Remove a node which causes an event //
    doc.getDocumentElement().removeChild(node);
    doc.getDocumentElement().appendChild(clone);
  }

  public void handleEvent(Event ev) {
    if (ev.getType().equals(MutationEventImpl.DOM_NODE_REMOVED)) {
      Node node = (Nodeev.getTarget();
      Node parentNode = node.getParentNode();
      System.out.println("Node " + node.getNodeName()
          " was removed from " + parentNode.getNodeName());
    }
    if (ev.getType().equals(MutationEventImpl.DOM_NODE_INSERTED)) {
      Node node = (Nodeev.getTarget();
      Node parentNode = node.getParentNode();
      System.out
          .println("Node " + node.getNodeName()
              " was inserted as a child of "
              + parentNode.getNodeName());
    }
  }
}

//game.xml
/*
<?xml version="1.0"?>
<games>
<game genre="shooter">XML Invaders</game>
<game genre="rpg">A Node in the XPath</game>
<game genre="action">XPath Racers</game>
</games>


*/

           
       
Related examples in the same category
1.  XML Document information by DOM
2.  Using DOM for Syntax Checking
3.  Using the DOM Parser to Build a Document Tree Using the DOM Parser to Build a Document Tree
4.  DOM Features DOM Features
5.  Searching through a document
6.  Check a vendor's DOM implementation Check a vendor's DOM implementation
7.  List an XML file after building it into a JDOM Document List an XML file after building it into a JDOM Document
8.  Make up and write an XML document, using JDOM Make up and write an XML document, using JDOM
9.  Make up and write an XML document, using DOM Make up and write an XML document, using DOM
10.  Simple demo of JDOM
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.