Version 34 of TclDOM

Updated 2008-02-06 17:08:10 by AK

TclDOM, maintained at http://sourceforge.net/projects/tclxml , is the DOM part of Explain's TclXML project.

 What: TclDOM
 Where: http://tclxml.sf.net/ 
        http://tclsoap.sf.net/tclxmldom25.zip 
 Description: TclDOM is a binding for the Document Object Model.
        TclDOM features live node lists and named node maps.
        The current version is v3.1 .
 Updated: 02/2004
 Contact: mailto:[email protected] 

TclDOM has partial pure-Tcl-coded XPath support, as of September 2001.

An example application using TclDOM/libxml2 is tkxmllint, a GUI for xmllint. For Mac OS X or Windows at http://tclxml.sourceforge.net/tkxmllint.html

Version 2.5 of TclDOM adds a couple of new example scripts that may be useful:

    domtext: displays a DOM document in a Tk Text widget
    domtree: displays a DOM document's tree structure in a
        BWidget Tree widget.

Version 3.0 includes the source script for tkxmllint (http://tclxml.sourceforge.net/tkxmllint.html ) application.


Newcomers may find the comparison TclDOM vs tDOM useful in deciding between the two competing packages.


Andreas Kupries provides a daily snapshot of the SourceForge CVS depot at http://www.purl.org/net/akupries/soft/cvs-snapshots/ .

AK: Note: There are more places to find Tcl binaries and sources than my site.

SRB: Most recent TclDOM distributions provide Mac OS X and MS Windows binaries.

ActiveState's ActiveTcl also includes this package.


Documentation appears (also?) at http://aspn.activestate.com/ASPN/Products/ActiveTcl/tcldom/index.html .


If you are trying to load TclDOM into a safe slave interpreter see the notes under TclXML. - PT


Steve Ball: "The only software in the TclXML stable that will validate a document is TclDOM/libxml2.". This package is also known as Tcldomxml. It wraps a TclDOM-API around GNOME's libxml2 [L1 ] and provides the basis for TclXSLT.

Version 3.0 provides XML Schema and RelaxNG validation, in addition to DTD validation.


CThatcher: TclDOM seems to be missing a few important features - like the ability to parse new data into an arbitrary place in a DOM document. The only parse feature creates a new document each time and most of the ::node:: operations (like appendChild) simply complain that the node needs to be in the same document. If anyone knows a quick and easy way to do the above with TclDOM please let me know.

Steve Ball: (Updated) DOM Level 3 introduces an "importNode" method that solves this problem. This has been implemented in TclDOM/tcl v3.0b2 (an implementation for TclDOM/libxml2 is on the TODO list).

CThatcher: Additionally, there should at least be an option for parsing that allows us to control the underlying parser before parsing actually takes place. Like the handling of whitespace. In *theory* we should be treating all whitespace as is, but in practice we need to be a little more brutal than that sometimes - and, let's face it, the trim function is diabolically slow.

Steve Ball: How about submitting an RFE to the TclXML SourceForge tracker? (Update: The TclDOM parse method (as of version 3.0b2) passes arguments through to the TclXML parser, so enabling/disabling ignorable-whitespace can be done)

CThatcher: Ok, thanks Steve; I'll stop ranting and start helping. Is there a tech document on how all the DOM structures work together internally, etc., as I may look into implementing importNode myself, but I'm lazy and don't want to spend hours of trawling the source :)

Steve Ball: Hmmm... no there isn't any technical doco. I'll put that on my to-do list.


lv there is something called tcldompro - is this an obsolete package? Does it provide functionality that is no longer needed or has been deprecated in favor of some other package?

Steve Ball: "tcldompro" is a C implementation of the TclDOM API. It is included in the TclDOM distribution (in the "src" directory), but not built by default. Originally written by Al Biggerstaff (when at Ajuba Solutions), it is currently maintained by Joe English.


Windows might require

    package require xml
    package require xml::libxml2
    package require dom

to load the package correctly.


Several aspects of TclDOM's documentation haven't kept pace with the package itself. Miscellaneous observations:

  • "The -attributes option of the node command gives you the name of a Tcl array (ie. it's a 'live' variable) that contains the attribute names and values."
  • ...

Steve Ball (2006-11-29): I just checked the v3.1 documentation online at tclxml.sf.net and it correctly documents the -attributes option.


This code will validate an XML document using a schema file.

    set fd [open "foo.xml"]
    set xml_instance [read $ch]
    close $fd
    set doc [dom::parse $xml_instance] ;# this is the instance XML document
    set fd [open "foo.xsd"]
    set schemaxml [read $fd]
    close $fd
    set schemadoc [dom::parse $schemaxml]        ;# this is the schema XML document
    $schemadoc schema compile
    if {[catch {$schemadoc schema validate $doc} msg]} {
        puts stderr "document is not schema-valid due to \"$msg\""
    } else {
        puts stderr "document is schema-valid"
    }

Could someone post an example of how getElementsByTagName works? Thanks.


Category PackageCategory XML