Support for the Document Object Model (DOM) in Allegro CL (added 7/29/04)

Support for the Document Object Model (DOM) is now available in Allegro CL. DOM provides a programmatic interface to XML and HTML. It is fully described in the Document Object Model (DOM) Level 1 Specification (Second Edition), Version 1.0, W3C Working Draft 29 September, 2000, available at www.w3.org/TR/2000/WD-DOM-Level-1-20000929/.

The introduction to that document describes the DOM project:

This specification defines the Document Object Model Level 1, a platform- and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure and style of documents. The Document Object Model provides a standard set of objects for representing HTML and XML documents, a standard model of how these objects can be combined, and a standard interface for accessing and manipulating them. Vendors can support the DOM as an interface to their proprietary data structures and APIs, and content authors can write to the standard DOM interfaces rather than product-specific APIs, thus increasing interoperability on the Web.

The goal of the DOM specification is to define a programmatic interface for XML and HTML. The DOM Level 1 specification is separated into two parts: Core and HTML [only the Core part is currently supported in Allegro CL]. The Core DOM Level 1 section provides a low-level set of fundamental interfaces that can represent any structured document, as well as defining extended interfaces for representing an XML document. These extended XML interfaces need not be implemented by a DOM implementation that only provides access to HTML documents; all of the fundamental interfaces in the Core section must be implemented. A compliant DOM implementation that implements the extended XML interfaces is required to also implement the fundamental Core interfaces, but not the HTML interfaces.

As noted, Allegro CL supports the Core part of DOM only. This DOM Level 1 implementation passes 155 key tests from the W3.org DOM conformance test suite. These tests were translated from the javascript source. The related SAX parser implementation in Allegro CL (see sax.htm) expands all entity references.

Here are some simple examples using the DOM in Allegro CL.

Examples

There are examples of using DOM in examples/xml/dom-code.cl (that file is downloaded with the DOM patch). Here are the examples in action:

;;
;; copyright (c) 2004 Franz Inc, Oakland, CA

(in-package :user)

;; The DOM module is part of the SAX module
(eval-when (compile load eval) (require :sax)) 

(defpackage :user (:use :net.xml.dom))

;;;
;;; Some programming examples using the DOM Level 1 API
;;;


(defparameter *dom-ex1-data*

  ;; Sample file noDTDXMLfile.xml from domtest-ecmascript-120499.zip 
  ;; at http://www.w3.org/DOM/Test/

"<?xml version='1.0'?>
<staff>
 <employee>
  <employeeId>EMP0001</employeeId>
  <name>Margaret Martin</name>
  <position>Accountant</position>           
  <salary>56,000</salary>
  <gender>Female</gender>
  <address domestic='Yes'>1230 North Ave. Dallas, Texas 98551</address>
 </employee>
 </staff>

"
)

(defun dom-ex1 ()
  
  ;; Parse an XML document and walk the resulting DOM tree

  (let* ((doc (parse-to-dom *dom-ex1-data*))
         (root (dom-document-element doc))
        )
    (format t "~%The root element is ~A~%" (dom-tag-name root))

    (dolist (c (dom-child-node-list root))
      (format t "~&  Child node: ~S~%" c))

    (let* ((addr (dom-list-elements-by-tag-name root "address"))
           (attrs (dom-attributes (first addr))))
      (format t "~&  The address element has ~S attributes~%" 
              (dom-length attrs)))
      
    doc))

(defun dom-ex2 (&optional (file t))

  ;; Build a DOM tree from parts and print the result

  (let ((doc (dom-create-document))
        staff emp)
    (dom-append-child doc (setf staff (dom-create-element doc "staff")))
    (dom-append-child staff (setf emp (dom-create-element doc "employee")))

    (flet ((add (parent doc name text &aux child)
                (dom-append-child parent (setf child 
                                               (dom-create-element doc name)))
                (dom-append-child child (dom-create-text-node doc text))
                child))
      (add emp doc "employeeId" "EMP00002")
      (add emp doc "name" "Alter Ego")
      (add emp doc "position" "Auditor")
      (add emp doc "salary" "65,000")
      (add emp doc "gender" "Male")
      (dom-set-attribute 
       (add emp doc "address" "Port Ludlow, WA")
       "domestic" "Yes")
      )

    (dom-print doc file)

    doc))

;;  Here we run the functions:

cl-user(18): (dom-ex1)

The root element is staff
  Child node: #<dom1-text -2- @ #x10ca93fa>
  Child node: #<dom1-element employee(13) @ #x10ca95ca>
  Child node: #<dom1-text -2- @ #x10caa4da>
  The address element has 1 attributes
#<dom1-document (1) @ #x10ca46c2>
cl-user(19): (dom-ex2)
<staff>
  <employee>
    <employeeId>EMP00002</employeeId>
    <name>Alter Ego</name>
    <position>Auditor</position>
    <salary>65,000</salary>
    <gender>Male</gender>
    <address domestic='Yes'>Port Ludlow, WA
    </address>
  </employee>
</staff>
#<dom1-document (1) @ #x11148e42>
cl-user(20): 

The DOM module is included with the Allegro 7.0 distribution and is available by patch for Allegro CL 6.2. The patch can be downloaded by sys:update-allegro.

The documentation for the DOM module is here for Allegro CL 7.0 and here for Allegro 6.2.

Copyright © 2023 Franz Inc., All Rights Reserved | Privacy Statement Twitter