|
|
Here's the whole proc if it helps to show what I am trying to do.
It's an xml to json converter. It's be first real attempt at anything
with tcl, so be gentle.
proc convertxml2json {doc {isrecursive "0"}} {
set type [$doc nodeType]
set name [$doc nodeName]
set value [$doc nodeValue]
#find out if the node has grandchildren
if { [$doc hasChildNodes] } {
set child [$doc firstChild]
set hasgrandchildren [$child hasChildNodes]
}
if { $isrecursive == 0 } {
append returnstring "\{"
}
if { $type == "ELEMENT_NODE" } {
append returnstring $name:
} else {
append returnstring $value
# here's where I am having trouble
if { [$doc nextSibling] != "" } {
append returnstring ","
}
}
if { $type == "ELEMENT_NODE" && [$doc hasChildNodes] &&
$hasgrandchildren == 1} {
append returnstring "\{"
}
foreach child [$doc childNodes] {
append returnstring [convertxml2json $child 1]
}
if { $type == "ELEMENT_NODE" && [$doc hasChildNodes] &&
$hasgrandchildren == 1} {
append returnstring "\}"
}
if { $isrecursive == 0 } {
append returnstring "\}"
}
return $returnstring
}
|
|