Version 10 of TclXML

Updated 2003-03-18 20:16:12

TclXML is Zveno's XML extension, based at http://sf.net/projects/tclxml/ .

As of September 2001, TclXML has a pure-Tcl XPath parser.

Packages build on top of this functionality are TclDOM and TclXSLT. All of these packages are part of the ActiveTcl Batteries Included distribution.


There have been some people having problems loading the TclDOM and TclXML packages into Safe interpreters. The problem is that the safe package restricts filenames to 14 characters. TclXML has tclparser-8.1.tcl as one of its files, which exceeds this limit. Here is the TclSOAP solution which removes this restriction:

 proc SOAP::CGI::createInterp {interp path} {
     set slave [safe::interpCreate $interp]
     safe::interpAddToAccessPath $slave $path
     # override the safe restrictions so we can load our
     # packages (actually the xml package files)
     proc ::safe::CheckFileName {slave file} {
        if {![file exists $file]} {error "file non-existent"}
        if {![file readable $file]} {error "file not readable"}
     }
     return $slave
 }

This returns a safe interpreter for which interp eval "package require xml" should work. PT


[As people keep turning up asking for tutorials on getting started with TclXML--the most basic things, like retrieval of one value from one tag of a single document--I want to make a point at least of putting a few links here which point to Steve's explanations in the mailing list.]

Dave Griffin offers a sample retrieval of values from a DOM tree:

    package require xml
    package require dom

    set xmlSrc {
        <?xml version="1.0" encoding="UTF-8"?>

        <SampleRequest>
            <PostingURL url="http://foo.com/some/service/url"/>
            <Password>FooBar</Password>
        </SampleRequest>
    }

    # First you parse the XML, the result is held in token d.
    set d [dom::DOMImplementation parse $xmlSrc]

    # One way in is to parse it by the assumed structure and
    # use the Document interface -style of query.  This code isn't
    # flexible at all and only highlights how the dom methods
    # can be used.

    # First find the SampleRequest element in the DOCUMENT
    set sr [dom::document getElementsByTagName $d SampleRequest]

    # Next retrieve the two sub-elements in SampleRequest
    set purl  [dom::element getElementsByTagName $sr PostingURL]
    set pword [dom::element getElementsByTagName $sr Password]

    # Now we will retrieve the url attribute of PostingURL
    set url [dom::element getAttribute $purl url]
    # url == "http://foo.com/some/service/url"
    puts "url = $url"

    # Finally, we want to retrieve the password.  This is non-obvious.
    # The value "FooBar" is actually in a "textNode" child of pword,
    # so you have to do ferret it out with generic node commands.
    set pwordv [dom::node children $pword]
    # You could have also used: dom::node cget $pword -firstchild

    # dom::node cget $pwordv -nodeType  -> textNode
    set password [dom::node cget $pwordv -nodeValue]
    # password == "FooBar"
    puts "password = $password"




Steve's starting to make courseware available through http://www.zveno.com/courses/samples/XML-App-Dev-Tcl/


Steve Ball [ Category Package | Category XML | ]