Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

package require tdom

set XML "


<inventory>
<order number='1'>
<customer>John Doe</customer>
<phone>555-4321</phone>
<email>jdoe@example.com</email>
<website/>
<parts>
<widget sku='XYZ123' />
<widget sku='ABC789' />
<widget sku='asagdsdh123' />
</parts>
</order>
<order number='2'>
<customer>Sorabh Dung</customer>
<phone>444-4321</phone>
<email>sd@example.com</email>
<website/>
<parts>
<widget sku='def123' />
<widget sku='asagh123' />
</parts>
</order>
</inventory>
"

set doc [dom parse $XML]


set root [$doc documentElement]

proc explore {parent} {


global cntr
set type [$parent nodeType]
set name [$parent nodeName]

if {$type != "ELEMENT_NODE"} {
puts "Node Name - [[$parent parentNode] nodeName] and its Value is
[$parent nodeValue]"
return
}

if {[llength [$parent attributes]]} {


puts "parent node - [$parent nodeName] , attributes: [join [$parent
attributes] ", "] and [$parent getAttribute [join [$parent attributes] ", "]]"
}

foreach child [$parent childNodes] {


incr cntr
explore $child
incr cntr -1
}

if {$cntr == 1} {
puts "------------"
}
}

explore $root

You might also like