|
|||||||||||||
|
|
|
|
|
|
|
|
|
||||||
|
|
|||||||||||||
![]() ![]() ![]() ![]() ![]() ![]() |
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:
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. ExamplesThere 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. Return to the Tech Corner page.
© 2008 Franz Inc - Privacy Statement |