Posted to tcl by patthoyts at Wed Jan 09 14:06:37 GMT 2008view raw

  1. A fragment that uses htmparse to extract the list of countries from some web page
  2. State is an application array name.
  3.  
  4. Its called using something like
  5. if {[catch {htmlparse::parse \
  6. -cmd [list ParseCountries ::State] $data} err]} {
  7. tk_messageBox -icon error -message $err -detail $::errorInfo
  8. }
  9.  
  10.  
  11. proc ParseCountries {State tag slash param text} {
  12. upvar #0 $State state
  13. variable CountryCodes
  14. set tag [string tolower $tag]
  15. if {$tag eq "hmstart"} { set state(c_state) 0}
  16. if {$tag eq "select"} {
  17. if {$slash eq "/"} {
  18. set state(c_state) 0
  19. } elseif {[string match "*PartyCountry*" $param]} {
  20. set state(c_state) 1
  21. }
  22. }
  23. if {$state(c_state) && $tag eq "option" && $slash eq {}} {
  24. foreach p $param {
  25. foreach {k v} [split $p =] break
  26. if {$k eq "value"} {
  27. set name [string toupper [string range $text 0 end-3]]
  28. set CountryCodes($name) [string trim $v "\""]
  29. }
  30. }
  31. }
  32. }