Opening a DOM Interface

Using DOM to access the structure of content typically require that you open a DOM interface to the content object. A wide range of objects can have a DOM interface, included:

The Common LISP binding of DOM provides a with-open-dom contruct to open a DOM interface to a content object. It binds a DOM node to a variable, evaluates a number of forms that access or modifies the object, then eventually saves the changes and closes the interface. By default, it opens an interface to access and navigate the content, without allowing any changes. The following example opens the index.html document of the default site and gets a list of all paragraph elements:

(with-open-dom (homepage "home:documents;index.html")
 (dom:get-elements-by-tag-name homepage "P"))

WITH-OPEN-DOM takes several keywords. These keywords depends on the type of the object or reference. For a file or stream, they are similar to as used in with-open-file:

Direction

Direction can be :Input, :Output or :IO. If it is :input (the default), the DOM interface is initialized with the content that are referenced. If it is output, a new document is created, with the reference set to the result. If the direction is IO, the document is initialized with the content from the reference, and changes will be persistant.

If Exists

If-exists can be :error or :supersede. If it is :error, an error is signalled if the referenced data object (e.g. a file) already exists. This is the default if direction is :output. If it is :supersede (the default), the new document will replace the referenced object.

If Does Not Exist

If-does-not-exist can be :error or :create. If it is :create, a new data object is created. This is the default if the direction is :output. If it :error (the default) an error is signalled if the document object does not exists.