comp.lang.tcl
[Top] [All Lists]

Re: tdom - not understanding "nextSibling"

Subject: Re: tdom - not understanding "nextSibling"
From: chedderslam
Date: Thu, 29 May 2008 14:34:01 -0700 PDT
Newsgroups: comp.lang.tcl

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
 }

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy