TCLDOM question - tagnames?
- From: "galen" <gjwilkerson@xxxxxxxxx>
- Date: 30 Jan 2007 15:48:44 -0800
Hi,
I have created the XML structure below...
However, I want to use the "class" attribute as the tag type.
For instance the tag that starts: (2nd tag)
<_Number_of_Trees min="-268435455.0" eval="DERIVED" value="0"
dims="0"
should instead read:
<VARIABLE name = "_Number_of_Trees" min="-268435455.0" eval="DERIVED"
value="0" dims="0"
etc.
I just don't understand how to use the TCLDOM commands to do this.
Best guess is:
dom::element configure $temp -tagName [$rc GetModelClass $path]
(full script at bottom)
thanks for any help.
-----
<?xml version='1.0'?>
<!DOCTYPE model>
<model numberOfTimeSteps="1" integrationMethod="Euler"
name="forestV4FP" numberOfPaths="16" timeUnits="unit">
<_Number_of_Trees min="-268435455.0" eval="DERIVED" value="0"
dims="0" path="/model/_Number_of_Trees" max="268435455.0"
class="VARIABLE" type="INTEGER"/>
<r min="0.0" eval="TABLE" value="10" dims="0" path="/model/r"
max="20.0" class="VARIABLE" type="INTEGER"/>
<Tree min="0.0" eval="SPLIT" value="{}" dims="MEMBERS 0" path="/
model/Tree" max="0.0" class="SUBMODEL" type="VALUELESS">
<Saplings min="-1e+100" eval="DERIVED" value="10.0" dims="0"
path="/model/Tree/Saplings" max="1e+100" class="IMMIGRATION"
type="REAL"/>
<Tree_Size min="-1e+100" eval="DERIVED" value="{}" dims="MEMBERS
0" path="/model/Tree/Tree_Size" max="1e+100" class="COMPARTMENT"
type="REAL"/>
<Growth_Rate min="-1e+100" eval="DERIVED" value="{}" dims="MEMBERS
0" path="/model/Tree/Growth_Rate" max="1e+100" class="FLOW"
type="REAL"/>
<Maximum_Growth_Rate min="-268435455.0" eval="DERIVED" value="{}"
dims="MEMBERS 0" path="/model/Tree/Maximum_Growth_Rate"
max="268435455.0" class="VARIABLE" type="INTEGER"/>
<Chance_of_Death min="-1e+100" eval="DERIVED" value="{}"
dims="MEMBERS 0" path="/model/Tree/Chance_of_Death" max="1e+100"
class="LOSS" type="REAL"/>
<X_Position min="-1e+100" eval="DERIVED" value="{}" dims="MEMBERS
0" path="/model/Tree/X_Position" max="1e+100" class="VARIABLE"
type="REAL"/>
<Y_Position min="-1e+100" eval="DERIVED" value="{}" dims="MEMBERS
0" path="/model/Tree/Y_Position" max="1e+100" class="VARIABLE"
type="REAL"/>
<ID min="-268435455.0" eval="DERIVED" value="{}" dims="MEMBERS 0"
path="/model/Tree/ID" max="268435455.0" class="VARIABLE"
type="INTEGER"/>
<branch min="0.0" eval="SPLIT" value="{}" dims="MEMBERS 0" path="/
model/Tree/branch" max="0.0" class="SUBMODEL" type="VALUELESS">
<length min="-268435455.0" eval="DERIVED" value="{}"
dims="MEMBERS 0" path="/model/Tree/branch/length" max="268435455.0"
class="VARIABLE" type="INTEGER"/>
</branch>
</Tree>
<mountain min="0.0" eval="SPLIT" value="sm" dims="0" path="/model/
mountain" max="0.0" class="SUBMODEL" type="VALUELESS">
<peak min="0.0" eval="SPLIT" value="sm" dims="0" path="/model/
mountain/peak" max="0.0" class="SUBMODEL" type="VALUELESS">
<glacier min="-268435455.0" eval="DERIVED" value="3" dims="0"
path="/model/mountain/peak/glacier" max="268435455.0" class="VARIABLE"
type="INTEGER"/>
</peak>
</mountain>
</model>
------------------------------------------
#!/usr/share/Simile47/System/bin/wish
# script to extract model paths and attributes
# from simile model and convert to XML
# using TCLDOM
set model_file "/home/simile/modeling/galen_wilkerson/ModelXML/
forestV4FP.sml"
set model_params "/home/simile/modeling/galen_wilkerson/ModelXML/
forestV4FP.spf"
# try GUMBO
#set model_file "/home/simile/modeling/GUMBO/GUMBOO2.sml"
puts "starting"
# load simile stuff
package require SimileAutoObj
lappend auto_path "/home/simile/src/Simile47/System/lib/"
puts "loading libraries"
# load DOM XML stuff
lappend auto_path "/usr/share/tcldom3.1/"
lappend auto_path "/usr/lib/Tclxml3.1/"
lappend auto_path "/usr/local/lib/tcllib1.9/"
package require dom
package require xpath
puts "libraries loaded"
similescript::ModelWindow modelWin
modelWin UseMRE false
modelWin Open $model_file
# load params if needed
modelWin LoadParams $model_params
modelWin Run
similescript::RunControl runControl
set rc runControl
#$rc GetPhaseCount
set paths [$rc GetAllPaths]
puts $paths
set model_string "/model"
set doc [dom::DOMImplementation create]
puts [::dom::DOMImplementation serialize $doc]
puts "--------------"
# set some attribute for model
set path $model_string
set node1 [dom::DOMImplementation createNode $doc $path]
# get the model filname, use as model name
set type "name"
set value $model_file
set values [split $value "/"]
set value [lindex $values end]
set values [split $value "."]
set value [lindex $values 0]
dom::element setAttribute $node1 $type $value
set type "integrationMethod"
set value [$rc GetIntegrationMethod]
dom::element setAttribute $node1 $type $value
set type "numberOfTimeSteps"
set value [$rc GetNumberOfTimeSteps]
dom::element setAttribute $node1 $type $value
set type "timeUnits"
set value [$rc GetTimeUnits]
dom::element setAttribute $node1 $type $value
# the total number of model components
set type "numberOfPaths"
set value [llength $paths ]
dom::element setAttribute $node1 $type $value
#################
# create nodes, get attributes and set them for each $path node
set class_channel [open "/home/simile/modeling/galen_wilkerson/
ModelXML/class_junk" w+]
foreach path $paths {
# change space " " to underscore "_"
# NOTE: THIS COULD CAUSE A CONFLICT BETWEEN TWO VARIABLES
# IF THE TWO VARIABLES ONLY DIFFER BY UNDERLINE, SPACE, or
ASTERISK
#regsub -all (\k) $path _ fixed_path
regsub -all {\s} $path _ fixed_path
regsub -all {[*]} $fixed_path _ fixed_path
regsub -all {[,]} $fixed_path _ fixed_path
set temp $model_string$fixed_path
#createNode token xpath
set node1 [dom::DOMImplementation createNode $doc $temp]
#configure token -tagName [name]
#dom::element configure $temp -tagName [$rc GetModelClass $path]
#setAttribute token name value
set tmp "path"
set value $temp
dom::element setAttribute $node1 $tmp $value
set type "type"
set value [$rc GetModelType $path]
dom::element setAttribute $node1 $type $value
set class "class"
set value [$rc GetModelClass $path]
dom::element setAttribute $node1 $class $value
set eval "eval"
set value [$rc GetModelEval $path]
dom::element setAttribute $node1 $eval $value
set dims "dims"
set value [$rc GetModelDims $path]
dom::element setAttribute $node1 $dims $value
set min "min"
set value [$rc GetMinValue $path]
dom::element setAttribute $node1 $min $value
set max "max"
set value [$rc GetMaxValue $path]
dom::element setAttribute $node1 $max $value
set component_value "value"
set value [$rc GetValue $path]
dom::element setAttribute $node1 $component_value $value
}
# print it out
puts [::dom::serialize $doc -indent true]
exit
.
- Prev by Date: Re: UI design for Tk app--PNG's or GIF's for icons?
- Next by Date: Re: if else else
- Previous by thread: tcl code parser for moinmoin wiki anyone?
- Next by thread: source cmd
- Index(es):
Relevant Pages
|