expat

Purpose: describe what expat is good for, and how Tcl programmers can make use of the binding.

Expat is an XML SAX parser written in C. tDOM includes it and a set of Tcl bindings .


Using Expat

This example was posted by George Petasis to comp.lang.tcl:

proc handle_start {name attributes args} {
  set ::CurrentTag $name
};# handle_start

proc handle_end {name args} {
  set ::CurrentTag {}
};# handle_end

proc handle_text {data args} {
  switch -exact $::CurrentTag {
    title {set ::CurrentTitle [string trim $data]}
    id    {set ::CurrentId    $data}
    text  {# do something with the text }
  }
};# handle_text

## Create a streaming xml parser...
package require tdom
expat xml -elementstartcommand  handle_start \
          -elementendcommand    handle_end \
          -characterdatacommand handle_text

## Open the input file (always in utf-8)
set fd [open $in]
fconfigure $fd -encoding utf-8

## Parse the xml data...
xml parsechannel $fd

## Done! Free parser and close file...
close $fd
xml free 

For expat usage examples, see: