Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a little proc...

Status
Not open for further replies.

sl1200mk2

Technical User
Jun 7, 2005
39
FR
here's an xml file:

<PDContainer type="h_list">
<element>
<s>step</s>
<f>0</f>
<s>mem</s>
<f>0</f>
<s>time-in</s>
<f>0</f>
<s>time-out</s>
<f>0</f>
<s>delay-in</s>
<f>0</f>
<s>delay-out</s>
<f>0</f>
<s>wait</s>
<f>0</f>
</element>
<element>
<s>step</s>
<f>1</f>
<s>mem</s>
<f>1</f>
<s>time-in</s>
<f>2</f>
<s>time-out</s>
<f>2</f>
<s>delay-in</s>
<f>0</f>
<s>delay-out</s>
<f>0</f>
<s>wait</s>
<f>0</f>
</element>
</PDContainer>

with the following code, i succeed to parse it in a text widget, and pushing the record button save the text back in xml format:
package require dom

toplevel .conduite
set var conduite
set viewFont "-*-courier-medium-r-normal--*-120-*-*-*-*-*-*"
frame .$var.view -bd 0 -relief flat
scrollbar .$var.vscroll -command ".$var.text yview"
text .$var.text -yscroll ".$var.vscroll set" -setgrid 1 -height 20 -width 80 \
-font $viewFont

pack .$var.view -expand yes -fill both
pack .$var.text -in .$var.view -side left -expand yes -fill both
pack .$var.vscroll -in .$var.view -side left -fill y
pack [button .$var.b -text "RECORD" -command SaveText ]
set ifd [open /Applications/D-light/data/conduite/new2-conduite.xml r]


set xml [read $ifd ]
set doc [dom::parse $xml ]
set csv {}

foreach total [dom::node selectNode $doc /PDContainer/element] {
append csv [dom::node stringValue $total]
}
.$var.text insert end $csv

package require tdom
set doc [dom createDocument PDContainer]
set root [$doc documentElement]
$root setAttribute type "h_list"
set rooty [dom createNodeCmd elementNode element]
dom createNodeCmd elementNode s
dom createNodeCmd elementNode f
dom createNodeCmd textNode t

set fileId [.conduite.text get 1.0 end]

proc SaveText {} {
global root fileId
$root appendFromScript {
element {
foreach coupe $fileId {
if {[string is integer $coupe]} {
f {t $coupe}
} else {
s {t $coupe}
}}
}
puts [$root asXML]
}
return
}
focus .$var.vscroll


but:
i don't succeed to make 2 <element> node as the original file.
i've tried to lookfor the blank space in the text widget, but it fail.

maybe the solution is to format the text widget horizontaly, and not vertically to, after, send each line to the SvaText proc.
but i don't know how to do.

please feel free to comment...
best regards
nicolas
 
in fact,
<element> node must be repeated each 14 value

so i need a loop wich act like that

- read text widget
- each 14 value, send content the 14 value to SaveText proc
- continue till there's no value

.....
 
after a working night, here's the result, it works exept i don't know how to remove the blank character at the end of each line of the text widget,
and also i've got an extra </element> at the end of the new xml file

any idea?

package require dom

toplevel .conduite
set var conduite
set viewFont "-*-courier-medium-r-normal--*-120-*-*-*-*-*-*"
frame .$var.view -bd 0 -relief flat
scrollbar .$var.vscroll -command ".$var.text yview"
text .$var.text -yscroll ".$var.vscroll set" -setgrid 1 -height 20 -width 80 \
-font $viewFont

pack .$var.view -expand yes -fill both
pack .$var.text -in .$var.view -side left -expand yes -fill both
pack .$var.vscroll -in .$var.view -side left -fill y
pack [button .$var.b -text "RECORD" -command create ]
set dir /Applications/D-light/data/conduite/new2-conduite.xml
set ifd [open $dir r]


set xml [read $ifd]
set doc [dom::parse $xml ]
set csv {}

foreach total [dom::node selectNode $doc /PDContainer/element] {
append csv [dom::node stringValue $total]
}
set csv2 [regexp -all -inline {\S+} $csv]
set k {}
foreach el $csv2 {
if { $el == "null"} {
append k \n
}
append k "$el "
}

.$var.text insert end [string map { {null } {} {wait \c} {nico}} $k]
set root {}

proc create {} {
package require tdom
global root
set doc [dom createDocument PDContainer]
set root [$doc documentElement]
$root setAttribute type "h_list"
dom createNodeCmd elementNode element
dom createNodeCmd elementNode s
dom createNodeCmd elementNode f
dom createNodeCmd textNode t
record
}

proc record {} {
SaveText [split [.conduite.text get 0.0 end] \n]
}


proc SaveText {data} {
global root
foreach resultat $data {
$root appendFromScript {
element {
foreach coupe $resultat {
if {[string is integer $coupe]} {
f {t $coupe}
} else {
s {t $coupe}
}
}
}
}
}
puts [$root asXML]
}

focus .$var.vscroll
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top