300 likes | 503 Views
Advanced DOM. Objectives. DOM Level 2 Modules DOM Level 2 API DOM Traversal DOM Ranges DOM Events Workshops. DOM2. The DOM Level 1 (DOM1) has two specifications Core and HTML The DOM Level 2 (DOM2) Is evolved from DOM1 Is built upon the Dom 1 interfaces
E N D
Objectives • DOM Level 2 Modules • DOM Level 2 API • DOM Traversal • DOM Ranges • DOM Events • Workshops
DOM2 • The DOM Level 1 (DOM1) has two specifications Core and HTML • The DOM Level 2 (DOM2) • Is evolved from DOM1 • Is built upon the Dom 1 interfaces • Can be considered as the super set of the DOM 1 • Has 6 different specifications • Core • Views • Style • Event • Traversal-Range • HTML • CSS
Core • Is the fundamental specification or module. • Defines a set of objects and interfaces to access and manipulate parsed XML content. • Has incorporated new ways to traverse and manipulate the XML documents through other optional modules. • Facilitates creating and populating a Document object through the DOM API calls. • Extends the functionality of the DOM core 1 with some added features.
View & Style • View • Provides interfaces to facilitate the presentation of XML documents. • Is optional in nature. Its implementation requires the implementation of the DOM 2 Core module. • Style • Provides interfaces to enable programmers to dynamically access and manipulate style sheets. • Is optional in nature. Its implementation requires the implementation of the DOM 2 Core module.
The event flow is a process that facilities movement of events starting from the DOM implementation to the DOM of an XML document The steps Generating each event by the DOM implementation. Directing the flow of the event towards the target of the event by the DOM implementation through its target attribute. Triggering the events registered on the target when the event reaches the target. Event flow
Traversal & HTML • Traversal • Allows programs and scripts to traverse through a DOM tree and identify a range of content in the document dynamically. • Allows the traversing the DOM tree to access the content in it. • Contains the TreeWalker, NodeIterator, and NodeFilter interfaces to facilitate easy traversal through the document content. • HTML • Allows programs and scripts to access and modify the content and structure of HTML documents dynamically. • Extends the interfaces defined in the DOM 1 HTML module.
CSS • Is an optional module in the DOM. Its implementation requires the implementation of the Core module. • To support its implementation, the hasFeature(feature, version) method needs to pass the feature as CSS and the version as 2.0.
DocumentTraversal interface • Contains the methods to create the objects to traversal through a DOM document tree. • Through the document traversal, the contents can be accessed and then modified as per the requirement
NodeFilter interface • Facilitates the creation of the object that will filter out specific nodes present in a NodeIterator or TreeWalker. • Is used through the NodeIterator or a TreeWalker. • Defines the acceptNode() method • Determines whether a specified node is part of the logical structure of a NodeIterator or TreeWalker. • Returns different types of constants to indicates whether the nodes will be considered as a part of the logical structure of a NodeIterator or a TreeWalker. • Returns FILTER_ACCEPT to indicate that the node is a part of the logical structure • Returns FILTER_REJECT and FILTER_SKIP to indicate that the node is not a part of the logical structure. • Syntax: public short acceptNode(Node n)
NodeIterator interface • Provides the NodeIterator object to traverse through a NodeList of a sub-tree.
NodeIterator interface (cont) • The traversal of the document involves the following steps • Step 1: Configure a NodeFilter object to determine which elements should be incorporated into the logical view of the document to be traversed. • Step 2: Parse the XML document to be traversed using an instance of DOMParser interface. • Step 3: Retrieve the parsed document through the getDocument() method of the DocumentImplementation interface. • Step 4: Create a NodeIterator by using the createNodeIterator() method found in the DocumentTraversal interface. • Step 5: Create an iterator from NodeIterator and identify the nodes to be set as visible and nodes to be set invisible by passing the constants from NodeFilter. • Step 6: Call the nextNode(), previousNode(), or detach() method to carry on traversing through the node list.
Example public class NodeIterator { public static void main (String[] args) { try{ DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance (); DocumentBuilder parser = dbf.newDocumentBuilder (); DOMImplementation impl=parser.getDOMImplementation (); if(!impl.hasFeature ("traversal", "2.0")){ System.out.println("A DOM implementation that supports traversal is required"); return;} Document doc = parser.parse (args[0]); DocumentTraversal traversal=(DocumentTraversal)doc; org.w3c.dom.traversal.NodeIterator iterator = traversal.createNodeIterator ( doc, NodeFilter.SHOW_ALL, null, true); System.out.println("Artists listed in the current catalog\n"); Node node; while((node=iterator.nextNode ())!=null){ if(node.getNodeName ().equals ("ARTIST")){ System.out.println("\t" + node.getTextContent ());} } }catch(Exception e){e.printStackTrace ();}} }
Example (cont) • catalog.xml <?xml version="1.0" encoding="UTF-8"?> <catalog> <cd> <ARTIST>Aptech</ARTIST> <COUNTRY>INDIA</COUNTRY> </cd> <cd> <ARTIST>FU</ARTIST> <COUNTRY>CANADA</COUNTRY> </cd> <cd> <ARTIST>Arena</ARTIST> <COUNTRY>INDIA</COUNTRY> </cd> <cd> <ARTIST>FPT</ARTIST> <COUNTRY>Vietnam</COUNTRY> </cd> </catalog>
TreeWalker interface • Provides a tree representation of the nodes in the document. • NodeIterator is presented as a list of nodes of a sub-tree • Can be traversed in the upward or downward direction through the sub-tree
TreeWalker interface (cont) • The traversal of the document involves the following steps • Step 1: Configure a NodeFilter object to determine which elements should be incorporated into the logical view of the document to be traversed. • Step 2: Parse the XML document to be traversed using an instance of the DOMParser interface. • Step 3: Retrieve the parsed document through the getDocument() method of the DocumentImplementation interface. • Step 4: Create a TreeWalker by using the createTreeWalker () method found in the DocumentTraversal interface. • Step 5: Create a walker from TreeWalker and identify the nodes to be set as visible and nodes to be set invisible by passing the constants from NodeFilter. • Step 6: Call the methods available in the TreeWalker interface to carry on traversing through the node list.
Example public class TreeWalkerEx { public static void main (String[] args) { try{ DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance (); DocumentBuilder parser = dbf.newDocumentBuilder (); Document doc = parser.parse (args[0]); DocumentTraversal traversal = (DocumentTraversal)doc; TreeWalker walker = traversal.createTreeWalker (doc.getDocumentElement (), NodeFilter.SHOW_ELEMENT, null, true); traverseNodes (walker, ""); }catch(Exception e){e.printStackTrace ();} } private static final void traverseNodes(TreeWalker walker, String indent){ Node node =walker.getCurrentNode (); System.out.println(indent + "- " + ((Element)node).getTagName ()); for(Node n = walker.firstChild (); n!=null;n=walker.nextSibling ()){ traverseNodes (walker, indent + "\t");} walker.setCurrentNode (node); } }
Example (cont) • catalog.xml <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/css" href=nutrition.css"?> <literature> <book> <name>JSP - Servlet</name> <author> <firstname>Aptech</firstname> <lastname>Indian</lastname> </author> <category>Technique</category> </book> <book> <name>Java</name> <author> <firstname>Aptech</firstname> <lastname>Indian</lastname> </author> <category>Language</category> </book> </literature>
Range interface • The Range object of the Range interface represents a fragment of a Document, DocumentFragment or Attr. • The fragment represents a range with specific boundary points and the content inside it.
Range interface (cont) • The steps to alter the position of a range • Create a range by the createRange() method. • Set the start and end boundary by the setStart() and setEnd() methods. • After the position of the position of the range by shifting its start and end positions • Deleting Content with Range • Select the content to be deleted from the range using the selectNode() method. • Delete the selected portion of the range by the deleteContents() method.
Example public class DOMRange { public static void main (String[] args) { try{ DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance (); DocumentBuilder parser = dbf.newDocumentBuilder (); Document doc = parser.parse (args[0]); Element order = doc.getDocumentElement (); System.out.println("Node name: " + order.getNodeName ()); DocumentRange range = (DocumentRange)doc; //create and attach range Range r = range.createRange (); r.setStartBefore (order.getFirstChild ()); r.setEndAfter (order.getLastChild ()); System.out.println("After creating: " + r.toString ()); //delete the order contents and detach the range from it r.deleteContents (); System.out.println("After deleting: " + r.toString ()); r.detach (); System.out.println("After detaching: " + r.toString ()); }catch(Exception e){e.printStackTrace ();}}}
Mouse event • The MouseEvent interface is defined in the Event module that is designed to deal with the mouse input device. • The documentEvent interface creates the MouseEvent, which is sent to the event handler. Then, the MouseEvent interface, using its initMouseEvent() method, initialized the MouseEvent. This method can only be called before MouseEvent starts towards the handler by the dispatchEvent() method. • The mouse events can be of different types
UI Events • The UIEvent interface handles events, which are generated by user interaction through an external device that can be a mouse, a key board, or a touch screen. • The DocumentEvent interface creates the UIEvent, an instance of the UIEvent interface. Through this object, the UIEvent interface manipulates the user interface events.
Mutation Events • To allow notification of any change to the structure of the document • MutationEvent is created by the DocumentEvent interface • MutationEvent object is initialized by the initMutationEvent() method.
WORKSHOP ACTIVITIES • Building the console Java application using DOM2 parser can do • Traversal Document using NodeIterator Interface • Traversal Document using TreeWalker Interface