Home > Articles

This chapter is from the book

This chapter is from the book

DOM Level II

The Document Object Model Level II (the DOMII) defines a set of interfaces to create, manipulate, track, and view an XML document. DOM Level II goes beyond Level I and, for the most part, fills in areas where it is clear that additional functionality is required.

Note

As of this writing, the DOM Level II specification is in the final stages of being accepted by the W3C. However, no implementations currently exist and, as a result, we can examine each of the areas covered by the specification but give no concrete examples. It's more than likely that by the time you have this book in hand implementations will exist. Because the specification itself is in its final stages and not likely to change the information significantly, this section can be thought of as a quick reference to the DOM Level II interfaces.

Specifically, the DOMII provides interfaces for the following:

  • HTML— A set of HTML-specific interfaces, such as HTMLDocument and HTMLElement, which allow for processing HTML documents by the DOM. We will not look closely at the HTML interfaces because they are outside the scope of this text.

  • Views— A set of interfaces for working with views. A view is like a window into a document. DOMII views are very much like the traditional concepts of a view with two different applications, or different objects in the same application, looking onto different parts of a document. A view could be thought of in terms of an HTML frame or a computed view after a stylesheet has been applied.

  • Stylesheets— The DOMII introduces a number of interfaces that represent the generic concept of a stylesheet. The DOMII specification provides for examining stylesheets in a generic fashion and treating them very much like XML Document objects.

  • CSS Stylesheets— Because CSS use is so prevalent, the DOMII contains a set of interfaces for accessing CSS stylesheets. We will only give light coverage of this area of the specification because it's superceded by XSL stylesheets.

  • Events— One of the long-awaited features of DOMII is support for events. The event interfaces cover such areas as basic event flow and event registration and define User Interface (UI), Mouse, Key, Mutation, and HTML events. We will concentrate on Mutation events because they are the most applicable to Java programming/XML.

  • Traversal— The DOMII defines a set of interfaces that are collectively lumped into the group Traversal. These interfaces, much like the JDK1.2 collections interfaces, define mechanisms for walking a tree in either tree or list order.

  • Range— The final area covered by the DOMII is Range. Ranges cover what we traditionally think of as selections and define the concepts of boundary elements and what is meant by a range of elements between two boundary elements.

Note

DOM implementations can be queried to determine if they support one or more of the previously mentioned features by using the hasFeature(DOMString) method on a DOMImplementation object. The DOMString returns true if the specified feature is supported.

View Interfaces

Perhaps the simplest set of interfaces in DOMII is the view interfaces. View defines a window or view onto a XML Document object. Views are optional and may not be provided by an implementation. There are two view interfaces:

  • AbstractView An abstract view contains a single, read-only attribute DocumentView document.

  • DocumentView The DocumentView interface is implemented by Document objects and contains a single, read-only attribute, AbstractView defaultView, which refers to the default document.

The view interfaces can be used collectively to get the original document from a view and views from documents.

Stylesheet Interfaces

There are a five stylesheet interfaces:

  • StyleSheet An interface representing a generic stylesheet. In XML documents, StyleSheet objects are created in reference to stylesheet processing instructions. The StyleSheet interface contains attributes:

    • DOMString type The type of the stylesheet (`test/css' for example).

    • Boolean disabled Is this stylesheet associated with a document?

    • Node ownerNode The document the stylesheet is associated with.

  • StyleSheetList An ordered list of all the StyleSheet objects. Contains a length attribute representing the number of stylesheets in the list and a StyleSheet item() method for returning a given stylesheet.

  • MediaList An unordered list of all the media type associated with a StyleSheet. The MediaList interface provides methods for accessing each of the media types associated with a stylesheet, as well as methods to add and delete media types.

  • LinkStyle The LinkStyle interface is provided so that the stylesheet object associated with a document can be retrieved, for example via a processing instruction. It contains a single attribute StyleSheet sheet.

  • DocumentStyle The DocumentStyle interface provides a mechanism for accessing stylesheets from a Document object and contains a single attribute StyleSheetList styleSheets. The DocumentStyle interface is normally implemented by Document objects.

Events

From a Java developer's perspective, events are one of the more interesting areas of the DOMII specification. Events are interesting because they define a mechanism whereby an application can determine easily at runtime how a Document is changing. Events fall into three broad categories:

  • UI Events— UI Events are those events generated by the user interface. UI events are typically the result of external devices, such as a mouse click or a key press.

  • UI Logical Events— UI Logical Events are a step above normal UI Events in that they detail higher level happenings, such as changes to fonts, pointer focus changes, and so on.

  • Mutation Events— From a Java/DOM perspective, Mutation Events are the most interesting type of event. Mutation events alert applications to changes in the underlying structure of a DOM Document. Every time a node is changed, added, or deleted, the potential exists for a UI event to occur.

There are three principal participants in DOM event parsing. Event producers, any class that implements the Node interface, generate events that can be consumed by event consumers. Event consumers are informed whenever something happens to an object to which they are listening. And Events themselves are passed from an event generator to an event consumer to inform a consumer what has happened. In fact, there is no reason that an event consumer could not produce events as well, perhaps to propagate information from one area of an application to another.

Because DOM Documents are tree structured, a single event type does not suit all event happenings. The DOMII specification defines three kinds of event processing:

  • Capturing— Capturing allows an event consumer to capture an event; that is, stop it from propagating from one event consumer to another.

  • Bubbling— Bubbling is the process whereby a single event at a leaf node is propagated or bubbled all the way up a tree from one node to its parent. When a node is bubbled, each event consumer is given the chance to act on it. Using bubbling, an application can register to receive an event on the root of a Document and see all events on that document's children.

  • Cancelable— Cancelable events can be stopped from being processed by a given event listener. Cancelable events are normally used to override the additional processing that might be incurred as an event bubbles up through a Document tree.

There are three main interfaces used with DOMII events.

  • EventTarget Node objects implement the EventTarget interface. The EventTarget interface contains three methods:

    • addEventListener(String type, EventListener listener, boolean useCapture) Registers classes to handle events

    • removeEventListener(String type, EventListener listener, boolean useCapture) Removes previously registered listeners

    • dispatchEvent(Event) Generates an event

  • EventListener Classes that want to consume events on a given Node or Document do so by implementing the EventListener interface. This interface has a single method, handleEvent(Event), which is called when an event happens.

  • Event The Event class itself is the currency that EventListeners and EventTargets trade in. Event objects contain a number of fields that the consumer can act on, such as the Node associated with the event—whether the event is cancelable or not, whether the event bubbles or not. The Event class also contains a number of methods that allow an EventListener to change how the event is processed. For example, the stopPropagation() method allows a listener to stop a bubbling event from being delivered to listeners registered higher up in the DOM tree.

There are a number of additional classes that derive from the Event class:

  • UIEvent Events that encapsulate additional information about the View in which they happened.

  • MouseEvent Events that contain pointer-related information, such as screen x/y position, and keyboard state, such as whether the Ctrl key was pressed or not, and so on.

  • MutationEvent Events that contain related Node information, previous and current value, and other applicable Node information. MutationEvents have a number of derived events, such as DOMSubtreeModified, DOMNodeInserted, DOMNodeRemoved, DOMAttrModified, and so on.

Traversal Interfaces

Anyone who has used the JDK1.2 collection classes will be comfortable with the DOMII Traversal interfaces. There are two main interfaces for Document traversal—NodeIterator and TreeWalker. A third interface, NodeFilter, allows developers to write filters to limit the num-ber of elements returned by the two traversal interfaces.

Iterators

The Iterator interface provides a mechanism whereby a developer can walk a flattened ver-sion of a Document. Iterators are created by calling the createNodeIterator() on a Document object as shown in the following:

						
NodeIterator iterator =  document.createNodeIterator(rootNode,whatToShow,filter);

where rootNode is the starting Node object, whatToShow is a bitwise ored combination from Table 3.2, and filter is any class that implements the NodeFilter interface or null.

There are three methods of note on the Iterator interface:

  • Node nextNode() Returns the next node in the list.

  • Node previousNode() Returns the previous node in the list.

  • void detach() Releases any resources associated with the Iterator. Subsequent calls to the iterator will raise an exception.

One of the interesting aspects of iterators is how they react in the face of change. Iterators are defined to represent the current structure of a Document tree and must correctly handle situa-tions where the previous or next node has been deleted or new nodes added.

Conceptually an Iterator has a concept of a current node. Consider, for a moment, the following list of nodes:

						
* A B C D E F G *

^(current node)

When the iterator is first created, the current node is null. Calls to getNext() would return A, and calls to getPrevious() would return null. Likewise, if the current node was after G, as shown next, getNext() would return null and getPrevious() would return G.

						
* A B C D E F G *

               ^(current node)

If a node was added or removed, the Iterator would still behave as you would expect with the list effectively "shifting" left or right as required. For example, assume we have the following situation:

						
* A B C D E F G *

       ^(current node)

where the current node D, and next returning E and previous returning C. If node E was removed, the Iterator would correctly return node F. If an additional node was inserted after D called D', the iterator would return D' for next.

In addition to the logical behavior of Iterators we've already seen, Node objects also have a behavior known as visibility. The second and third parameters to the createNodeIterator() method are a bit mask and a filter, respectively. These two parameters define whether a node is visible and will be returned by getNext() or getPrevious(). The functioning of the bit mask is fairly common. For example, if you only want TEXT and COMMENT nodes, provide a mask of NodeFilter.SHOW_TEXT | NodeFilter.SHOW_COMMENT. Or in additional constants to return other Elements of the Document. We will examine filters shortly.

TableÊNodeFilter Constants

Constant Description
NodeFilter.SHOW_ALL Return all elements
NodeFilter.SHOW_ELEMENT Return Element objects
NodeFilter.SHOW_ATTRIBUTE Return Attribute objects
NodeFilter.SHOW_TEXT Return Text objects
NodeFilter.SHOW_CDATA_SECTION Return CDATA objects
NodeFilter.SHOW_ENTITY_REFERENCE Return EntityReference objects
NodeFilter.SHOW_ENTITY Return Entity objects
NodeFilter.SHOW_PROCESSING_INSTRUCTION Return ProcessingInstruction objects
NodeFilter.SHOW_COMMENT Return Comment objects
NodeFilter.SHOW_DOCUMENT_TYPE Return DocumentType objects
NodeFilter.SHOW_DOCUMENT_FRAGMENT Return DocumentFragment objects
NodeFilter.SHOW_NOTATION Return Notation objects

TreeWalkers

TreeWalkers provide a mechanism whereby developers can walk the actual tree structure of a Document.

TreeWalkers are created by calling the createTreeWalker() method on a Document object, as shown in the following:

						
    TreeWalker walker =  document.createTreeWalker(rootNode,whatToShow,filter);

where rootNode is the starting Node object, whatToShow is a bitwise ored combination from Table 3.2, and filter is any class that implements the NodeFilter interface or null.

There are seven methods of note on the TreeWalker interface:

  • Node parentNode() Returns the parent of the current node

  • Node firstChild() Returns the first child of the current node

  • Node lastChild() Returns the last child of the current node.

  • Node previousSibling() Returns the sibling logically to the left of the current node.

  • Node nextSibling() Returns the sibling logically to the right of the current node.

  • Node previousNode() Returns the previous node in the document. Note that previousNode() and nextNode() can move up and down the tree, whereas the sibling methods will not.

  • Node nextNode() Returns the next node in the tree, moving down the tree as required.

If we consider the following tree:

						
                A
              B   C
             D E  F G
            H I

and B is the current node:

  • getParent() returns A

  • previousSibling() returns null

  • nextSibling() returns C

  • firstChild() returns D

  • lastChild() returns E

  • previousNode() returns A

  • nextNode() returns C

Filters

The final interface in the Iterator set of interfaces is the NodeFilter interface. Node filters provide developers with a finer level of control over when Elements are returned by Iterators and TreeWalkers by allowing a developer to examine a node and return NodeFilter.FILTER_ACCEPT, NodeFilter.FILTER_REJECT, or NodeFilter.FILTER_SKIP to have the Iterator or TreeWalker return, ignore, or skip a given node. The NodeFilter interface has a single method, short acceptNode(Node node), and must return one of the three previously listed constants.

For example, the following snippet of code defines a node filter that returns comment nodes but ignores all other nodes:

						
Class commentsOnlyFilter implements NodeFilter
{
    short acceptNode(Node n)
    {
        if (n instanceof Comment)
            return NodeFilter.FILTER_ACCEPT;
        else
            return NodeFilter.FILTER_SKIP;
    }
}

It's important to understand the difference between FILTER_SKIP and FILTER_REJECT. When walking a tree, if a filter returns FILTER_REJECT, the node will be rejected as well as all of its children. With FILTER_SKIP, the node will be rejected but its children will still be processed. For iterators FILTER_SKIP and FILTER_REJECT have the same behavior.

DOM Ranges

DOMII ranges behave in a fashion similar to what you would expect from a word processor or text editor. Ranges have what is termed a mark or start of range and an offset or end of range. DOMII ranges allow a number of logical operations to be performed on a Document object, such as adding a new element (insertNode()), deleting elements (deleteContents()), and copying elements (cloneRange()).

We create a range as follows:

					
Document d;
Range range = (DocumentRange)d.createRange();

After we have a range object, we then can set its start and end points using the following methods, all of which are from the Range interface:

  • Range.setStartBefore(Node startNode) Sets the start of the range to be before the current node, including the current node.

  • void setStartAfter(Node startNode) Sets the start of the range to be after the current node and not include the current node.

  • void setEndAfter(Node endNode) Sets the end of the range to be after the current node, including the given node.

  • void setEndBefore(Node endNode) Sets the end of the range to be before the endNode and not include the end node.

After we have a range of nodes, we can manipulate its content using the following:

  • void deleteContents() Deletes all Nodes between the starting and ending points from the Document object.

  • DocumentFragment extractContents() Creates a new DocumentFragment that represents the contents of the range. The extracted contents are deleted from the Document object.

  • DocumentFragment cloneContents() Creates a new DocumentFragment that is a copy of the contents of the range. The original Document is unchanged.

  • void InsertNode(Node new) Inserts the node into the range. The newly inserted node becomes the new start of the range.

Using range objects, we can manipulate the contents of a DOM tree easily using a graphical metaphor.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020