xml2dict

Difference between version 0 and 0 - Previous - Next
WJG (14/11/21) 

Needed to find a reliable way of extracting values from a largish XML database (48Mb). This was the quickest and simplest way of solving the problem. The sample data was obtained from https://www.w3schools.com/xml/cd_catalog.xml.

======
<company>DECCA</company>
<price>9.90</price>
<year>1991</year>
</cd>
<cd>
<title>The dock of the bay</title>
<artist>Otis Redding</artist>
<country>USA</country>
<company>Stax Records</company>
<price>7.90</price>
<year>1968</year>
</cd>
<cd>
<title>Picture book</title>
<artist>Simply Red</artist>
<country>EU</country>
<company>Elektra</company>
<price>7.20</price>
<year>1985</year>
</cd>
<cd>
<title>Red</title>
<artist>The Communards</artist>
<country>UK</country>
<company>London</company>
<price>7.80</price>
<year>1987</year>
</cd>
<cd>
<title>Unchain my heart</title>
<artist>Joe Cocker</artist>
<country>USA</country>
<company>EMI</company>
<price>8.20</price>
<year>1987</year>
</cd>
</catalog>
======



======
# !/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

package require tdom
package require json

set fp [open cd_catalog.xml r]
set XML [read $fp]
close $fp

## Convert set XML formatted data to a Tcl dict
# use tdom to convert XML to JSON
# use json to convert JSON to dict
# @param[in] xml        the xml text to convert
# returns data in xml as an enumberated dict
proc xml2dict { xml } {
        
        set root [[dom parse $xml] documentElement]

        set i -1
        foreach node [$root childNodes] { lappend res [incr i] [::json::json2dict [$node asJSON] ] }

        return $res

}


set DICT [xml2dict $XML]

puts [dict get $DICT 12]
======


returns:

======

title {For the good times} artist {Kenny Rogers} country UK company {Mucik Master} price 8.70 year 1995
======