#!/bin/sh # Copyright for the Uniform Repository Service (c) 1995 - 2004, # by Gerald Banon. All rights reserved. # Version 2.1 # parseXml.tcl \ exec ../../../../../\ dpi.inpe.br/banon/1997/10.21.17.24\ /doc/tk8.0/unix/wish "$0" ${1+"$@"} set example { abc } source cgi/mirrorfind-.tcl ;# Load source utilities1.tcl ;# Store # Load C:/tmp/curriculo.xml example # puts $example if [info exists array] {unset array} proc ProcessTag {parentName tag} { global array global tagNumber if [regexp {([^ ]*) (.*)} $tag m tagName attributePart] { regsub -all {=} $attributePart { } attributeList array set attributeArray $attributeList foreach attributeName [array names attributeArray] { if {[string compare {} $parentName] == 0} { set array($tagName|$tagNumber|,$attributeName) $attributeArray($attributeName) } else { set array($parentName.$tagName|$tagNumber|,$attributeName) $attributeArray($attributeName) } } return $tagName } else { return $tag } } proc ParseXml {parentName xml} { global array global tagNumber if [expr [info level] == 1] {set tagNumber 0} if [expr [info level] > 99] {return} # puts [list level: [info level]] set xml [string trim $xml "\n "] regsub {<\?[^?]*\?>} $xml {} rest set i 1 while {$i < 99 && [string compare {} $rest] != 0} { # puts [list i: $i] if [regexp {<([^>]*)>(.*)} $rest m tag rest2] { incr tagNumber if [regsub {/$} $tag {} tag] { ProcessTag $parentName $tag set rest $rest2 } else { set tagName [ProcessTag $parentName $tag] # if [expr [info level] == 2] {return} # regexp "(.*)(.*)" $rest2 m tagValue rest ;# time consuming # regexp "(.*)" $rest2 m tagValue # if [expr [info level] == 3 && $i == 2] {puts $rest2; puts $tagName} # regexp "(.*)" $rest2 m rest regexp -indices "(.*)" $rest2 m rest # regsub "$m$" $rest2 {} tagValue ;# doesn't work - tcl aborts set tagValue [string range $rest2 0 [expr [lindex $m 0] - 1]] set rest [string range $rest2 [lindex $rest 0] [lindex $rest 1]] if {[string compare {} $parentName] == 0} { ParseXml $tagName|$tagNumber| $tagValue } else { ParseXml $parentName.$tagName|$tagNumber| $tagValue } } set rest [string trim $rest "\n "] } else { set array($parentName) $rest set rest {} } incr i } } set errorInfo {} catch {ParseXml {} $example} puts $errorInfo set list [array get array] # Store list C:/tmp/curriculoList # puts [array get array CURRICULO-VITAE|1|.PRODUCAO-BIBLIOGRAFICA|68|.LIVROS-E-CAPITULOS|479|.LIVROS-PUBLICADOS-OU-ORGANIZADOS|480|.LIVRO-PUBLICADO-OU-ORGANIZADO|526|*] puts [array get array] puts done