By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,255 Members | 2,773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,255 IT Pros & Developers. It's quick & easy.

TCL/PHP/XML problem: I need to convert an XML file into a TCL list

P: n/a
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML
file into a TCL list as follows:

attr1 {val1} attr2 {val2} ... attrN {valN}

This is the TCL code that does this:

[TCL]
set contents [read $fileID [file size ${fileName}.xml]]; close $fileID
if {![string equal $switch -body]} {
# ONLY DO THIS IF THE XML CONTENTS CONTAIN NO BODY - WILL UPGRADE AT
A LATER DATE 11/24/2006 - PHIL
global serverName
if {![info exists serverName]} {
global env
source ./cgi_globals.tcl
global serverName
}

if {[string length [info procs {URL_ENCODE}]] == 0} { source
../url_procs.tcl }; # INCLUDE NEW url_procs.tcl URL TCL LIBRARY
if {[string length [info procs {IS_LIST}]] == 0} { source
{./tcl_string_tools.tcl} }; # INCLUDE THE TCL STRING LIBRARY CONTAINING
PROC is_list IF !FOUND

# BLOCK TO CREATE THE PHP SCRIPT TO PIPE INTO php.exe
regsub -all {'} [XML_CLEANUP_ATTRIBUTE $contents] {\"}
phpEscapedContents
regsub -all {"} $phpEscapedContents {\"} phpEscapedContents
set php {<? }
append php [subst { if (@is_file("./functions.inc.php")) [format %c
123] }]
append php { require_once("./functions.inc.php"); }
append php [subst { echo xml_to_tcl_list("$phpEscapedContents"); }]
append php [subst {[format %c 125] }]
append php { ?>}
set cannotRunPHP [catch {exec "echo '$php' | php" >@stdout
2>&stderr} errorMsg]
if {$cannotRunPHP} {
puts $errorMsg
return {}
} else {
if {![IS_LIST $contentsList]} { set contentsList [split
$contentsList] }
if {[llength $contentsList] == 0} { lappend contentsList {?} }
return [lrange $contentsList [expr {[lsearch -exact $contentsList
"?"] + 1}] end]
}

}
[/TCL]

The command-line PHP should look like this:

[PHP]
<? if (@is_file("./functions.inc.php")) {
require_once("./functions.inc.php"); echo
xml_to_tcl_list("$phpEscapedContents"); } ?>
[/PHP]

Note that $phpEscapedContents is a TCL variable, not a PHP variable

Ok, so this is what should happen:

1) PHP function xml_to_tcl_list will convert the inputted escaped XML
string into a TCL list
2) I need to return that TCL list into $contentsList and return it so
the proc returns a TCL list

However, it never gets that far, here is the error I get:

couldn't execute "echo '<? if (@is_file("./functions.inc.php")) {
require_once("./functions.inc.php"); echo xml_to_tcl_list("&lt;?xml
version=&quot;1.0&quot; encod": file name too long
I am sure I am going at this the wrong way, but I can't understand the
right way (TCL DOM parsing is far FAR beyond my ability to understand,
I've read manuals, tutorials, books, to no avail: I simply don't
understand how TCL can ever parse XML, I only understand simple PHP
parsing XML)

So at this point, all I want to do is convert an XML string into a TCL
list, and the only way I know how to do it is via PHP, but I can't get
all three languages to cooperate.

Help appreciated.

Thanx
Phil

Nov 25 '06 #1
Share this Question
Share on Google+
27 Replies


P: n/a
comp.lang.tcl wrote:
set cannotRunPHP [catch {exec "echo '$php' | php" >@stdout
2>&stderr} errorMsg]
You need to re-read the man page on exec. The first argument to exec is
a filename that will be exec'd; it is not a command line. If you want to
run a command line you should exec a command line processor (/bin/sh,
command.com, whatever).

Since "php" is what you want to exec, and $php is what you want to send
to its stdin, use "<<" (documented on the exec man page):

if {[catch {exec php << $php} result]} {
puts "error exec'ing php: $resulut"
} else {
puts "php result: $result"
}

You can read a wiki page on the subject here: http://mini.net/tcl/exec
Nov 25 '06 #2

P: n/a

Bryan Oakley wrote:
comp.lang.tcl wrote:
set cannotRunPHP [catch {exec "echo '$php' | php" >@stdout
2>&stderr} errorMsg]

You need to re-read the man page on exec. The first argument to exec is
a filename that will be exec'd; it is not a command line. If you want to
run a command line you should exec a command line processor (/bin/sh,
command.com, whatever).

Since "php" is what you want to exec, and $php is what you want to send
to its stdin, use "<<" (documented on the exec man page):

if {[catch {exec php << $php} result]} {
puts "error exec'ing php: $resulut"
} else {
puts "php result: $result"
}

Now I get this error:

couldn't execute "php": no such file or directory

When I try this:

[TCL]
set cannotRunPHP [catch {exec exec php << $php >@stdout 2>&stderr}
errorMsg]
if {$cannotRunPHP} {
puts $errorMsg
return {}
} else {
if {![IS_LIST $contentsList]} { set contentsList [split
$contentsList] }
if {[llength $contentsList] == 0} { lappend contentsList {?} }
return [lrange $contentsList [expr {[lsearch -exact $contentsList
"?"] + 1}] end]
}

[/TCL]

Wow this should only take an hour, it's taken 3 days so far!

Phil
>
You can read a wiki page on the subject here: http://mini.net/tcl/exec
Nov 25 '06 #3

P: n/a
comp.lang.tcl schrieb:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML
file into a TCL list as follows:

attr1 {val1} attr2 {val2} ... attrN {valN}

This is the TCL code that does this:

[TCL]
set contents [read $fileID [file size ${fileName}.xml]]; close $fileID
if {![string equal $switch -body]} {
# ONLY DO THIS IF THE XML CONTENTS CONTAIN NO BODY - WILL UPGRADE AT
A LATER DATE 11/24/2006 - PHIL
global serverName
if {![info exists serverName]} {
global env
source ./cgi_globals.tcl
global serverName
}

if {[string length [info procs {URL_ENCODE}]] == 0} { source
./url_procs.tcl }; # INCLUDE NEW url_procs.tcl URL TCL LIBRARY
if {[string length [info procs {IS_LIST}]] == 0} { source
{./tcl_string_tools.tcl} }; # INCLUDE THE TCL STRING LIBRARY CONTAINING
PROC is_list IF !FOUND

# BLOCK TO CREATE THE PHP SCRIPT TO PIPE INTO php.exe
regsub -all {'} [XML_CLEANUP_ATTRIBUTE $contents] {\"}
phpEscapedContents
regsub -all {"} $phpEscapedContents {\&quot;} phpEscapedContents
set php {<? }
append php [subst { if (@is_file("./functions.inc.php")) [format %c
123] }]
append php { require_once("./functions.inc.php"); }
append php [subst { echo xml_to_tcl_list("$phpEscapedContents"); }]
append php [subst {[format %c 125] }]
append php { ?>}
set cannotRunPHP [catch {exec "echo '$php' | php" >@stdout
2>&stderr} errorMsg]
if {$cannotRunPHP} {
puts $errorMsg
return {}
} else {
if {![IS_LIST $contentsList]} { set contentsList [split
$contentsList] }
if {[llength $contentsList] == 0} { lappend contentsList {?} }
return [lrange $contentsList [expr {[lsearch -exact $contentsList
"?"] + 1}] end]
}

}
[/TCL]

The command-line PHP should look like this:

[PHP]
<? if (@is_file("./functions.inc.php")) {
require_once("./functions.inc.php"); echo
xml_to_tcl_list("$phpEscapedContents"); } ?>
[/PHP]

Note that $phpEscapedContents is a TCL variable, not a PHP variable

Ok, so this is what should happen:

1) PHP function xml_to_tcl_list will convert the inputted escaped XML
string into a TCL list
2) I need to return that TCL list into $contentsList and return it so
the proc returns a TCL list

However, it never gets that far, here is the error I get:

couldn't execute "echo '<? if (@is_file("./functions.inc.php")) {
require_once("./functions.inc.php"); echo xml_to_tcl_list("&lt;?xml
version=&quot;1.0&quot; encod": file name too long

I am sure I am going at this the wrong way, but I can't understand the
right way (TCL DOM parsing is far FAR beyond my ability to understand,
I've read manuals, tutorials, books, to no avail: I simply don't
understand how TCL can ever parse XML, I only understand simple PHP
parsing XML)

So at this point, all I want to do is convert an XML string into a TCL
list, and the only way I know how to do it is via PHP, but I can't get
all three languages to cooperate.
If you showed us part of the XML there surely is a shorter way with one
of the XML parsers for Tcl like tdom, but maybe your just confused by
the difference between DOM and SAX style xml parsing.

Michael
Nov 25 '06 #4

P: n/a

Michael Schlenker wrote:
comp.lang.tcl schrieb:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML
file into a TCL list as follows:

attr1 {val1} attr2 {val2} ... attrN {valN}

This is the TCL code that does this:

[TCL]
set contents [read $fileID [file size ${fileName}.xml]]; close $fileID
if {![string equal $switch -body]} {
# ONLY DO THIS IF THE XML CONTENTS CONTAIN NO BODY - WILL UPGRADE AT
A LATER DATE 11/24/2006 - PHIL
global serverName
if {![info exists serverName]} {
global env
source ./cgi_globals.tcl
global serverName
}

if {[string length [info procs {URL_ENCODE}]] == 0} { source
./url_procs.tcl }; # INCLUDE NEW url_procs.tcl URL TCL LIBRARY
if {[string length [info procs {IS_LIST}]] == 0} { source
{./tcl_string_tools.tcl} }; # INCLUDE THE TCL STRING LIBRARY CONTAINING
PROC is_list IF !FOUND

# BLOCK TO CREATE THE PHP SCRIPT TO PIPE INTO php.exe
regsub -all {'} [XML_CLEANUP_ATTRIBUTE $contents] {\"}
phpEscapedContents
regsub -all {"} $phpEscapedContents {\&quot;} phpEscapedContents
set php {<? }
append php [subst { if (@is_file("./functions.inc.php")) [format %c
123] }]
append php { require_once("./functions.inc.php"); }
append php [subst { echo xml_to_tcl_list("$phpEscapedContents"); }]
append php [subst {[format %c 125] }]
append php { ?>}
set cannotRunPHP [catch {exec "echo '$php' | php" >@stdout
2>&stderr} errorMsg]
if {$cannotRunPHP} {
puts $errorMsg
return {}
} else {
if {![IS_LIST $contentsList]} { set contentsList [split
$contentsList] }
if {[llength $contentsList] == 0} { lappend contentsList {?} }
return [lrange $contentsList [expr {[lsearch -exact $contentsList
"?"] + 1}] end]
}

}
[/TCL]

The command-line PHP should look like this:

[PHP]
<? if (@is_file("./functions.inc.php")) {
require_once("./functions.inc.php"); echo
xml_to_tcl_list("$phpEscapedContents"); } ?>
[/PHP]

Note that $phpEscapedContents is a TCL variable, not a PHP variable

Ok, so this is what should happen:

1) PHP function xml_to_tcl_list will convert the inputted escaped XML
string into a TCL list
2) I need to return that TCL list into $contentsList and return it so
the proc returns a TCL list

However, it never gets that far, here is the error I get:

couldn't execute "echo '<? if (@is_file("./functions.inc.php")) {
require_once("./functions.inc.php"); echo xml_to_tcl_list("&lt;?xml
version=&quot;1.0&quot; encod": file name too long
I am sure I am going at this the wrong way, but I can't understand the
right way (TCL DOM parsing is far FAR beyond my ability to understand,
I've read manuals, tutorials, books, to no avail: I simply don't
understand how TCL can ever parse XML, I only understand simple PHP
parsing XML)

So at this point, all I want to do is convert an XML string into a TCL
list, and the only way I know how to do it is via PHP, but I can't get
all three languages to cooperate.
If you showed us part of the XML there surely is a shorter way with one
of the XML parsers for Tcl like tdom, but maybe your just confused by
the difference between DOM and SAX style xml parsing.
There is, but I've said this to nearly ever TCL and PHP developer on
earth: I do not understand XML parsing at all, DOM, SAX, xQuery,
X[whatever else], none of it makes sense to me and I cannot understand
the books, tutorials and online guides (not to mention the tips from
far smarter people than I) that has barraged me; all of it makes
absolutely no sense to me.

At this point the only way I can parse XML is using PHP, because that's
literally the ONLY way I can do it, period!

But if you want to see a sample of the XML I'm working with, here it
is:

<?xml version="1.0" encoding="utf-8" ?><trivia><entry id="1101"
triviaID="233" question="Who wrote &quot;Trilogy of Knowledge&quot;?"
answerID="1" correctAnswerID="1" answer="Believer"
expDate="1139634000"></entry><entry id="1102" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="2"
correctAnswerID="1" answer="Saviour Machine"
expDate="1139634000"></entry><entry id="1103" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="3"
correctAnswerID="1" answer="Seventh Avenue"
expDate="1139634000"></entry><entry id="1104" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="4"
correctAnswerID="1" answer="Inevitable End"
expDate="1139634000"></entry><entry id="1105" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="5"
correctAnswerID="1" answer="No such song existed"
expDate="1139634000"></entry>

Phil

>
Michael
Nov 25 '06 #5

P: n/a
comp.lang.tcl wrote:
Bryan Oakley wrote:
>>comp.lang.tcl wrote:
>> set cannotRunPHP [catch {exec "echo '$php' | php" >@stdout
2>&stderr} errorMsg]

You need to re-read the man page on exec. The first argument to exec is
a filename that will be exec'd; it is not a command line. If you want to
run a command line you should exec a command line processor (/bin/sh,
command.com, whatever).

Since "php" is what you want to exec, and $php is what you want to send
to its stdin, use "<<" (documented on the exec man page):

if {[catch {exec php << $php} result]} {
puts "error exec'ing php: $resulut"
} else {
puts "php result: $result"
}

Now I get this error:

couldn't execute "php": no such file or directory

When I try this:

[TCL]
set cannotRunPHP [catch {exec exec php << $php >@stdout 2>&stderr}
errorMsg]
if {$cannotRunPHP} {
puts $errorMsg
return {}
} else {
if {![IS_LIST $contentsList]} { set contentsList [split
$contentsList] }
if {[llength $contentsList] == 0} { lappend contentsList {?} }
return [lrange $contentsList [expr {[lsearch -exact $contentsList
"?"] + 1}] end]
}

[/TCL]

Wow this should only take an hour, it's taken 3 days so far!
When you are learning a new language, it always takes much longer to
solve problems. And in your case I'm afraid, you aren't taking much time
to actually learn the language which is compounding the problem.

The answer to your current problem is well documented in the exec man
page, and on the exec pages on the tcler's wiki. The problem you are
encountering is very simple: you are telling it to execute "php" but tcl
can't find "php". The short answer is, you either have to explicitly
tell the exec command which *file* to execute (eg: exec
/usr/local/bin/php), or you have to make sure that the file you tell it
exists somewhere within a directory defined in your PATH environment
variable.

Phil, I'm sorry you're having so many problems. You are correct that
this probably should have taken only an hour or two for a seasoned
programmer, but you are not a seasoned programmer and properly parsing
XML is, generally speaking, a Hard Problem. At least, it's hard when you
choose to not use proper XML parsing solutions and instead rely on a
series of text transformations.

Tcl is a very powerful language, but it doesn't have built-in facilities
to parse XML. This is not a weakness of Tcl, it's just the way it is.
The beauty of Tcl is, people can write extensions to the language to do
just about anything, including parsing XML. Unfortunately, you are
choosing not to take the time to learn how to use them.

I'll again recommend trying the xml2list function documented here:
http://mini.net/tcl/3919. I'm guessing that will work good enough for
you. Fortunately, it appears you're working on a simply hobby
application rather than a commercial application so we don't have to
worry so much about robustness.

Nov 25 '06 #6

P: n/a

Bryan Oakley wrote:
comp.lang.tcl wrote:
Bryan Oakley wrote:
>comp.lang.tcl wrote:

set cannotRunPHP [catch {exec "echo '$php' | php" >@stdout
2>&stderr} errorMsg]

You need to re-read the man page on exec. The first argument to exec is
a filename that will be exec'd; it is not a command line. If you want to
run a command line you should exec a command line processor (/bin/sh,
command.com, whatever).

Since "php" is what you want to exec, and $php is what you want to send
to its stdin, use "<<" (documented on the exec man page):

if {[catch {exec php << $php} result]} {
puts "error exec'ing php: $resulut"
} else {
puts "php result: $result"
}


Now I get this error:

couldn't execute "php": no such file or directory

When I try this:

[TCL]
set cannotRunPHP [catch {exec exec php << $php >@stdout 2>&stderr}
errorMsg]
if {$cannotRunPHP} {
puts $errorMsg
return {}
} else {
if {![IS_LIST $contentsList]} { set contentsList [split
$contentsList] }
if {[llength $contentsList] == 0} { lappend contentsList {?} }
return [lrange $contentsList [expr {[lsearch -exact $contentsList
"?"] + 1}] end]
}

[/TCL]

Wow this should only take an hour, it's taken 3 days so far!

When you are learning a new language, it always takes much longer to
solve problems. And in your case I'm afraid, you aren't taking much time
to actually learn the language which is compounding the problem.

The answer to your current problem is well documented in the exec man
page, and on the exec pages on the tcler's wiki. The problem you are
encountering is very simple: you are telling it to execute "php" but tcl
can't find "php". The short answer is, you either have to explicitly
tell the exec command which *file* to execute (eg: exec
/usr/local/bin/php), or you have to make sure that the file you tell it
exists somewhere within a directory defined in your PATH environment
variable.

Phil, I'm sorry you're having so many problems. You are correct that
this probably should have taken only an hour or two for a seasoned
programmer, but you are not a seasoned programmer and properly parsing
XML is, generally speaking, a Hard Problem. At least, it's hard when you
choose to not use proper XML parsing solutions and instead rely on a
series of text transformations.
Bryan, you're going to die when you hear this: I have 9+ years web
development experience, been doing TCL for about 7 years (mostly via
Vignette), have 5 certifications including a master PHP certification..
and I have no clue how to do any of this. Thus I am a seasoned
programmer (though honestly nobody here will ever recognize or believe
that)

Tcl is a very powerful language, but it doesn't have built-in facilities
to parse XML. This is not a weakness of Tcl, it's just the way it is.
The beauty of Tcl is, people can write extensions to the language to do
just about anything, including parsing XML. Unfortunately, you are
choosing not to take the time to learn how to use them.
I've said this over and over again: it's not that I won't take the
time, it's that I CANNOT do it because I have a learning disability
called Attention Deficit Disorder which psychologically does not permit
me to take the time to learn it. I am unable to understand man pages
like you and Cameron and other can do in your sleep. I read the Tcl
extensions online but don't understand them. The pages are in
Hungarian to me. I can't fathom them, I don't know how to install
them, manipulate them, use them, anything. If people could show me a
very very simple way of parsing this:

<?xml version="1.0" encoding="utf-8" ?><trivia><entry id="1101"
triviaID="233" question="Who wrote &quot;Trilogy of Knowledge&quot;?"
answerID="1" correctAnswerID="1" answer="Believer"
expDate="1139634000"></entry><entry id="1102" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="2"
correctAnswerID="1" answer="Saviour Machine"
expDate="1139634000"></entry><entry id="1103" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="3"
correctAnswerID="1" answer="Seventh Avenue"
expDate="1139634000"></entry><entry id="1104" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="4"
correctAnswerID="1" answer="Inevitable End"
expDate="1139634000"></entry><entry id="1105" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="5"
correctAnswerID="1" answer="No such song existed"
expDate="1139634000"></entry></trivia>

Into this:

id 1101 triviaID 233 question {Who wrote &quot;Trilogy of
Knowledge&quot;?} answerID 1 correctAnswerID 1 answer Believer expDate
113963500

[etc]

Then that would do it, but I guess the problem is that I can't explain
my problem, nor my disabilities, well enough for the programming
community to fathom my dilemma.

Phil
>
I'll again recommend trying the xml2list function documented here:
http://mini.net/tcl/3919. I'm guessing that will work good enough for
you. Fortunately, it appears you're working on a simply hobby
application rather than a commercial application so we don't have to
worry so much about robustness.
Nov 25 '06 #7

P: n/a
comp.lang.tcl wrote:
... I do not understand XML parsing at all, DOM, SAX, xQuery,
X[whatever else], none of it makes sense to me and I cannot understand
the books, tutorials and online guides (not to mention the tips from
far smarter people than I) that has barraged me; all of it makes
absolutely no sense to me.

At this point the only way I can parse XML is using PHP, because that's
literally the ONLY way I can do it, period!

But if you want to see a sample of the XML I'm working with, here it
is:

<?xml version="1.0" encoding="utf-8" ?><trivia><entry id="1101"
triviaID="233" question="Who wrote &quot;Trilogy of Knowledge&quot;?"
answerID="1" correctAnswerID="1" answer="Believer"
expDate="1139634000"></entry><entry id="1102" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="2"
correctAnswerID="1" answer="Saviour Machine"
expDate="1139634000"></entry><entry id="1103" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="3"
correctAnswerID="1" answer="Seventh Avenue"
expDate="1139634000"></entry><entry id="1104" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="4"
correctAnswerID="1" answer="Inevitable End"
expDate="1139634000"></entry><entry id="1105" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="5"
correctAnswerID="1" answer="No such song existed"
expDate="1139634000"></entry>
That data is mal-formed XML. For example, you are missing the closing
</triviatag.

Here's a solution that works with the above data. I've mentioned the
"xml2list" proc a couple of times, but with the sample data I see your
data will need a little extra pre-processing.

Step 1: copy the proc "xml2list" from this page: http://mini.net/tcl/3919

Second, enter the following, which is taking the above data verbatim and
storing it in a variable:

set data {<?xml version="1.0" encoding="utf-8" ?><trivia><entry
id="1101"
triviaID="233" question="Who wrote &quot;Trilogy of Knowledge&quot;?"
answerID="1" correctAnswerID="1" answer="Believer"
expDate="1139634000"></entry><entry id="1102" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="2"
correctAnswerID="1" answer="Saviour Machine"
expDate="1139634000"></entry><entry id="1103" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="3"
correctAnswerID="1" answer="Seventh Avenue"
expDate="1139634000"></entry><entry id="1104" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="4"
correctAnswerID="1" answer="Inevitable End"
expDate="1139634000"></entry><entry id="1105" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="5"
correctAnswerID="1" answer="No such song existed"
expDate="1139634000"></entry>}

Your data is missing an ending </triviatag, so we have to add it for
this specific example. I don't know if this is a problem you'll have to
solve with your full dataset. Also, the xml2list proc doesn't like the
leading <?xml...stuff. So, let's modify your data:

# remove the leading <?xml...?data
regexp {<\?.*?\?>(.*$)} $data -- data

# add a trailing </triviawhich is missing from
# the sample data
set data "$data</trivia>"

And now, convert it to a list and print it out:

set result [xml2list $data]
puts $result

If you didn't introduce any typos, you'll get the following output:

trivia {} {{entry {id 1101 triviaID 233 question {Who wrote
&quot;Trilogy of Knowledge&quot;?} answerID 1 correctAnswerID 1 answer
Believer expDate 1139634000} {}} {entry {id 1102 triviaID 233 question
{Who wrote &quot;Trilogy of Knowledge&quot;?} answerID 2 correctAnswerID
1 answer {Saviour Machine} expDate 1139634000} {}} {entry {id 1103
triviaID 233 question {Who wrote &quot;Trilogy of Knowledge&quot;?}
answerID 3 correctAnswerID 1 answer {Seventh Avenue} expDate 1139634000}
{}} {entry {id 1104 triviaID 233 question {Who wrote &quot;Trilogy of
Knowledge&quot;?} answerID 4 correctAnswerID 1 answer {Inevitable End}
expDate 1139634000} {}} {entry {id 1105 triviaID 233 question {Who wrote
&quot;Trilogy of Knowledge&quot;?} answerID 5 correctAnswerID 1 answer
{No such song existed} expDate 1139634000} {}}}

The above is a valid tcl list that you can now process with normal tcl
list-handling commands. Do *not* process this list with string
transformations (such as converting &quot; to a quote). If you do, you
run the risk of breaking it's list-ness. Instead, loop over the data and
do the conversion as a final step on a element-by-element basis.

Does this help? It's not robust; the xml2list assumes you have proper
xml with a balanced set of tags (or in the specific case in this
message, with a missing </triviatag). Hopefully, though, it will at
least get you started.
Nov 25 '06 #8

P: n/a

Bryan Oakley wrote:
comp.lang.tcl wrote:
... I do not understand XML parsing at all, DOM, SAX, xQuery,
X[whatever else], none of it makes sense to me and I cannot understand
the books, tutorials and online guides (not to mention the tips from
far smarter people than I) that has barraged me; all of it makes
absolutely no sense to me.

At this point the only way I can parse XML is using PHP, because that's
literally the ONLY way I can do it, period!

But if you want to see a sample of the XML I'm working with, here it
is:

<?xml version="1.0" encoding="utf-8" ?><trivia><entry id="1101"
triviaID="233" question="Who wrote &quot;Trilogy of Knowledge&quot;?"
answerID="1" correctAnswerID="1" answer="Believer"
expDate="1139634000"></entry><entry id="1102" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="2"
correctAnswerID="1" answer="Saviour Machine"
expDate="1139634000"></entry><entry id="1103" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="3"
correctAnswerID="1" answer="Seventh Avenue"
expDate="1139634000"></entry><entry id="1104" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="4"
correctAnswerID="1" answer="Inevitable End"
expDate="1139634000"></entry><entry id="1105" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="5"
correctAnswerID="1" answer="No such song existed"
expDate="1139634000"></entry>

That data is mal-formed XML. For example, you are missing the closing
</triviatag.

Here's a solution that works with the above data. I've mentioned the
"xml2list" proc a couple of times, but with the sample data I see your
data will need a little extra pre-processing.

Step 1: copy the proc "xml2list" from this page: http://mini.net/tcl/3919
Ok done
>
Second, enter the following, which is taking the above data verbatim and
storing it in a variable:

set data {<?xml version="1.0" encoding="utf-8" ?><trivia><entry
id="1101"
triviaID="233" question="Who wrote &quot;Trilogy of Knowledge&quot;?"
answerID="1" correctAnswerID="1" answer="Believer"
expDate="1139634000"></entry><entry id="1102" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="2"
correctAnswerID="1" answer="Saviour Machine"
expDate="1139634000"></entry><entry id="1103" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="3"
correctAnswerID="1" answer="Seventh Avenue"
expDate="1139634000"></entry><entry id="1104" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="4"
correctAnswerID="1" answer="Inevitable End"
expDate="1139634000"></entry><entry id="1105" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="5"
correctAnswerID="1" answer="No such song existed"
expDate="1139634000"></entry>}

Your data is missing an ending </triviatag, so we have to add it for
this specific example. I don't know if this is a problem you'll have to
solve with your full dataset. Also, the xml2list proc doesn't like the
leading <?xml...stuff. So, let's modify your data:

Sorry that was my fault, I left off the </triviatag when I copied and
pasted onto here. The closing tags do exist in all of my XML files
>
# remove the leading <?xml...?data
regexp {<\?.*?\?>(.*$)} $data -- data

# add a trailing </triviawhich is missing from
# the sample data
set data "$data</trivia>"

And now, convert it to a list and print it out:

set result [xml2list $data]
puts $result

If you didn't introduce any typos, you'll get the following output:

No I don't, I get the following error, spawned from within xml2list:

unmatched open quote in list while executing "lindex $item 0"
("default" arm line 2) invoked from within "switch -regexp -- $item {
^# {append res "{[lrange $item 0 end]} " ; #text item} ^/ { regexp
{/(.+)} $item -..." (procedure "xml2list" line 9)

This is what I did:

[TCL]
# USE xml2list PROC WITHIN THIS LIBRARY AS YOUR DEFAULT MEANS OF
PARSING XML INTO TCL LIST
if {![string equal $switch -body] && [string length [info procs
{xml2list}]] 0} {
regexp {<\?.*?\?>(.*$)} $contents -- contents
return [xml2list $contents]
}
[/TCL]
>
trivia {} {{entry {id 1101 triviaID 233 question {Who wrote
&quot;Trilogy of Knowledge&quot;?} answerID 1 correctAnswerID 1 answer
Believer expDate 1139634000} {}} {entry {id 1102 triviaID 233 question
{Who wrote &quot;Trilogy of Knowledge&quot;?} answerID 2 correctAnswerID
1 answer {Saviour Machine} expDate 1139634000} {}} {entry {id 1103
triviaID 233 question {Who wrote &quot;Trilogy of Knowledge&quot;?}
answerID 3 correctAnswerID 1 answer {Seventh Avenue} expDate 1139634000}
{}} {entry {id 1104 triviaID 233 question {Who wrote &quot;Trilogy of
Knowledge&quot;?} answerID 4 correctAnswerID 1 answer {Inevitable End}
expDate 1139634000} {}} {entry {id 1105 triviaID 233 question {Who wrote
&quot;Trilogy of Knowledge&quot;?} answerID 5 correctAnswerID 1 answer
{No such song existed} expDate 1139634000} {}}}

The above is a valid tcl list that you can now process with normal tcl
list-handling commands. Do *not* process this list with string
transformations (such as converting &quot; to a quote). If you do, you
run the risk of breaking it's list-ness. Instead, loop over the data and
do the conversion as a final step on a element-by-element basis.

Does this help? It's not robust; the xml2list assumes you have proper
xml with a balanced set of tags (or in the specific case in this
message, with a missing </triviatag). Hopefully, though, it will at
least get you started.
Like I said, I do have balanced XML (just didn't produce it here tis
all), but xml2list produces errors when I try to read it

Phil

Nov 25 '06 #9

P: n/a
comp.lang.tcl wrote:
id 1101 triviaID 233 question {Who wrote &quot;Trilogy of
Knowledge&quot;?} answerID 1 correctAnswerID 1 answer Believer expDate
113963500
Take the zip file I posted. Install it. Call

set x [::xsxp::parse $myXml]
return [lindex [lindex $x 2] 1]

(I think that's the right path thru the list to the attributes of the
first child element. Otherwise, print out the list and see which
elements you have to grab.)

--
Darren New / San Diego, CA, USA (PST)
Scruffitarianism - Where T-shirt, jeans,
and a three-day beard are "Sunday Best."
Nov 25 '06 #10

P: n/a

Darren New wrote:
comp.lang.tcl wrote:
id 1101 triviaID 233 question {Who wrote &quot;Trilogy of
Knowledge&quot;?} answerID 1 correctAnswerID 1 answer Believer expDate
113963500

Take the zip file I posted. Install it. Call
Sorry there is no attachment, no zip file of any kind.

Phil

>
set x [::xsxp::parse $myXml]
return [lindex [lindex $x 2] 1]

(I think that's the right path thru the list to the attributes of the
first child element. Otherwise, print out the list and see which
elements you have to grab.)

--
Darren New / San Diego, CA, USA (PST)
Scruffitarianism - Where T-shirt, jeans,
and a three-day beard are "Sunday Best."
Nov 25 '06 #11

P: n/a
In article <tt*****************@newssvr21.news.prodigy.net> ,
Bryan Oakley <oa****@bardo.clearlight.comwrote:
Nov 25 '06 #12

P: n/a

Cameron Laird wrote:
In article <tt*****************@newssvr21.news.prodigy.net> ,
Bryan Oakley <oa****@bardo.clearlight.comwrote:
.
.
.
# add a trailing </triviawhich is missing from
# the sample data
set data "$data</trivia>"
.
.
.
Alternatively, as Bryan knows, but doubtless eschewed for
pedagogic reasons,

append data </trivia>
Why? The XML files are not being auto-spawned by TCL or anything else!

Nov 25 '06 #13

P: n/a
comp.lang.tcl wrote:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML
file into a TCL list as follows:

attr1 {val1} attr2 {val2} ... attrN {valN}
First off, sorry to everyone else for the long post, but I got tired reading
these threads.

Second off, the following does not return *EXACTLY* what you asked for it
returns more, but you should be able to convert it to what you ask for.

##
## A node will be the following list of name value pairs:
## NAME nodeName
## TEXT text
## ATTRIBUTES attributeNameValueList
## CHILDREN childNodeList
##
package require tdom

set xml {<?xml version="1.0" encoding="utf-8" ?>
<trivia>
<entry id="1101" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="1" correctAnswerID="1" answer="Believer"
expDate="1139634000"></entry>
<entry id="1102" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="2" correctAnswerID="1" answer="Saviour
Machine" expDate="1139634000"</entry>
<entry id="1103" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="3" correctAnswerID="1" answer="Seventh
Avenue" expDate="1139634000"></entry>
<entry id="1104" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="4" correctAnswerID="1" answer="Inevitable
End" expDate="1139634000"></entry>
<entry id="1105" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="5" correctAnswerID="1" answer="No such song
existed" expDate="1139634000"></entry>
</trivia>
}

##
## Convert a node to a list
##
proc NodeToList {node} {
##
## Get the name and text value of the node
##
set name [$node nodeName]
set text [$node text]

##
## Get the attributes of the node
##
set attrList {}
foreach attribute [$node attributes] {
lappend attrList $attribute [$node getAttribute $attribute]
}

##
## Get the children of the node
##
set childrenList {}
foreach child [$node childNodes] {
if {![string equal [$child nodeType] TEXT_NODE]} then {
lappend childrenList [NodeToList $child]
}
}

##
## All done so return the list representing this subtree
##
return[list NAME $name TEXT $text ATTRIBUTES $attrList CHILDREN
$childrenList]
}

##
## Convert the XML to a DOM tree
##
dom parse $xml doc

##
## No get the root element
##
$doc documentElement root

##
## Convert the tree to a list
##
set results [NodeToList $root]
$doc delete
--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
Nov 25 '06 #14

P: n/a

Gerald W. Lester wrote:
comp.lang.tcl wrote:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML
file into a TCL list as follows:

attr1 {val1} attr2 {val2} ... attrN {valN}

First off, sorry to everyone else for the long post, but I got tired reading
these threads.

Second off, the following does not return *EXACTLY* what you asked for it
returns more, but you should be able to convert it to what you ask for.

##
## A node will be the following list of name value pairs:
## NAME nodeName
## TEXT text
## ATTRIBUTES attributeNameValueList
## CHILDREN childNodeList
##
package require tdom
Sorry I am unable to figure out how to install and use tdom. I tried
this

lappend auto_path /home/ppowell/web/cgi-bin
package require tdom

at the top of xml_procs.tcl, to no avail, I get the following error:

can't find package tdom while executing "package require tdom" (file
"xml_procs.tcl" line 2)

I was able to download and install tdom, or I guess install it:

1) I downloaded it

2) I unzipped it

3) everything's in /home/ppowell/web/cgi-bin/tDom-0.8.0

4) I was not allowed to do ../configure, much less "make" nor "make
install", "../configure" not found

Phil
>
set xml {<?xml version="1.0" encoding="utf-8" ?>
<trivia>
<entry id="1101" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="1" correctAnswerID="1" answer="Believer"
expDate="1139634000"></entry>
<entry id="1102" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="2" correctAnswerID="1" answer="Saviour
Machine" expDate="1139634000"</entry>
<entry id="1103" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="3" correctAnswerID="1" answer="Seventh
Avenue" expDate="1139634000"></entry>
<entry id="1104" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="4" correctAnswerID="1" answer="Inevitable
End" expDate="1139634000"></entry>
<entry id="1105" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="5" correctAnswerID="1" answer="No such song
existed" expDate="1139634000"></entry>
</trivia>
}

##
## Convert a node to a list
##
proc NodeToList {node} {
##
## Get the name and text value of the node
##
set name [$node nodeName]
set text [$node text]

##
## Get the attributes of the node
##
set attrList {}
foreach attribute [$node attributes] {
lappend attrList $attribute [$node getAttribute $attribute]
}

##
## Get the children of the node
##
set childrenList {}
foreach child [$node childNodes] {
if {![string equal [$child nodeType] TEXT_NODE]} then {
lappend childrenList [NodeToList $child]
}
}

##
## All done so return the list representing this subtree
##
return[list NAME $name TEXT $text ATTRIBUTES $attrList CHILDREN
$childrenList]
}

##
## Convert the XML to a DOM tree
##
dom parse $xml doc

##
## No get the root element
##
$doc documentElement root

##
## Convert the tree to a list
##
set results [NodeToList $root]
$doc delete
--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
Nov 25 '06 #15

P: n/a
CORRECTION:

Now I get this:

couldn't read file "/home/ppowell/web/cgi-bin/tDOM-0.8.0/win/tdom.tcl":
no such file or directory while executing "source
/home/ppowell/web/cgi-bin/tDOM-0.8.0/win/tdom.tcl" ("package ifneeded"
script) invoked from within "package require tdom" (file
"xml_procs.tcl" line 2)

When I try this:

lappend auto_path /home/ppowell/web/cgi-bin/tDOM-0.8.0
package require tdom

comp.lang.tcl wrote:
Gerald W. Lester wrote:
comp.lang.tcl wrote:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML
file into a TCL list as follows:
>
attr1 {val1} attr2 {val2} ... attrN {valN}
First off, sorry to everyone else for the long post, but I got tired reading
these threads.

Second off, the following does not return *EXACTLY* what you asked for it
returns more, but you should be able to convert it to what you ask for.

##
## A node will be the following list of name value pairs:
## NAME nodeName
## TEXT text
## ATTRIBUTES attributeNameValueList
## CHILDREN childNodeList
##
package require tdom

Sorry I am unable to figure out how to install and use tdom. I tried
this

lappend auto_path /home/ppowell/web/cgi-bin
package require tdom

at the top of xml_procs.tcl, to no avail, I get the following error:

can't find package tdom while executing "package require tdom" (file
"xml_procs.tcl" line 2)

I was able to download and install tdom, or I guess install it:

1) I downloaded it

2) I unzipped it

3) everything's in /home/ppowell/web/cgi-bin/tDom-0.8.0

4) I was not allowed to do ../configure, much less "make" nor "make
install", "../configure" not found

Phil

set xml {<?xml version="1.0" encoding="utf-8" ?>
<trivia>
<entry id="1101" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="1" correctAnswerID="1" answer="Believer"
expDate="1139634000"></entry>
<entry id="1102" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="2" correctAnswerID="1" answer="Saviour
Machine" expDate="1139634000"</entry>
<entry id="1103" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="3" correctAnswerID="1" answer="Seventh
Avenue" expDate="1139634000"></entry>
<entry id="1104" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="4" correctAnswerID="1" answer="Inevitable
End" expDate="1139634000"></entry>
<entry id="1105" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="5" correctAnswerID="1" answer="No such song
existed" expDate="1139634000"></entry>
</trivia>
}

##
## Convert a node to a list
##
proc NodeToList {node} {
##
## Get the name and text value of the node
##
set name [$node nodeName]
set text [$node text]

##
## Get the attributes of the node
##
set attrList {}
foreach attribute [$node attributes] {
lappend attrList $attribute [$node getAttribute $attribute]
}

##
## Get the children of the node
##
set childrenList {}
foreach child [$node childNodes] {
if {![string equal [$child nodeType] TEXT_NODE]} then {
lappend childrenList [NodeToList $child]
}
}

##
## All done so return the list representing this subtree
##
return[list NAME $name TEXT $text ATTRIBUTES $attrList CHILDREN
$childrenList]
}

##
## Convert the XML to a DOM tree
##
dom parse $xml doc

##
## No get the root element
##
$doc documentElement root

##
## Convert the tree to a list
##
set results [NodeToList $root]
$doc delete
--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
Nov 25 '06 #16

P: n/a


package require tdom

set doc [dom parse {<?xml version="1.0" encoding="utf-8" ?><trivia><entry
id="1101"
triviaID="233" question="Who wrote &quot;Trilogy of Knowledge&quot;?"
answerID="1" correctAnswerID="1" answer="Believer"
expDate="1139634000"></entry><entry id="1102" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="2"
correctAnswerID="1" answer="Saviour Machine"
expDate="1139634000"></entry><entry id="1103" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="3"
correctAnswerID="1" answer="Seventh Avenue"
expDate="1139634000"></entry><entry id="1104" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="4"
correctAnswerID="1" answer="Inevitable End"
expDate="1139634000"></entry><entry id="1105" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="5"
correctAnswerID="1" answer="No such song existed"
expDate="1139634000"></entry></trivia>}]

set res ""
foreach domNode [$doc selectNodes ./*/*/attribute::*] {
eval lappend res $domNode
}
puts $res

--->

id 1101 triviaID 233 question {Who wrote "Trilogy of Knowledge"?} answerID
1 correctAnswerID 1 answer Believer expDate 1139634000 id 1102 triviaID
233 question {Who wrote "Trilogy of Knowledge"?} answerID 2
correctAnswerID 1 answer {Saviour Machine} expDate 1139634000 id 1103
triviaID 233 question {Who wrote "Trilogy of Knowledge"?} answerID 3
correctAnswerID 1 answer {Seventh Avenue} expDate 1139634000 id 1104
triviaID 233 question {Who wrote "Trilogy of Knowledge"?} answerID 4
correctAnswerID 1 answer {Inevitable End} expDate 1139634000 id 1105
triviaID 233 question {Who wrote "Trilogy of Knowledge"?} answerID 5
correctAnswerID 1 answer {No such song existed} expDate 1139634000
Ramon Ribó

En Sat, 25 Nov 2006 19:53:21 +0100, comp.lang.tcl
<ph**************@gmail.comescribió:
of parsing this:

<?xml version="1.0" encoding="utf-8" ?><trivia><entry id="1101"
triviaID="233" question="Who wrote &quot;Trilogy of Knowledge&quot;?"
answerID="1" correctAnswerID="1" answer="Believer"
expDate="1139634000"></entry><entry id="1102" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="2"
correctAnswerID="1" answer="Saviour Machine"
expDate="1139634000"></entry><entry id="1103" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="3"
correctAnswerID="1" answer="Seventh Avenue"
expDate="1139634000"></entry><entry id="1104" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="4"
correctAnswerID="1" answer="Inevitable End"
expDate="1139634000"></entry><entry id="1105" triviaID="233"
question="Who wrote &quot;Trilogy of Knowledge&quot;?" answerID="5"
correctAnswerID="1" answer="No such song existed"
expDate="1139634000"></entry></trivia>

Into this:
Nov 26 '06 #17

P: n/a
I've said this over and over again: it's not that I won't take the
time, it's that I CANNOT do it because I have a learning disability
called Attention Deficit Disorder which psychologically does not permit
me to take the time to learn it.
I'm sorry to hear that you have problems, but is that an excuse not to
RTFM?

I'll try and helpout with a simple solution. Note that your input XML
and the result that you want is very, how do I put it?, weird. This
solution will not suit many other applications (iow, don't try this at
home, kids).

Your source XML looks like this:
<?xml version="1.0" encoding="utf-8" ?><trivia><entry id="1101"
triviaID="233" question="Who wrote &quot;Trilogy of Knowledge&quot;?"
answerID="1" correctAnswerID="1" answer="Believer"
expDate="1139634000"></entry>
....

So, a bunch of <entryelements where the real data is in attributes.
This looks like a dump of a relational database to me.
Into this:

id 1101 triviaID 233 question {Who wrote &quot;Trilogy of
Knowledge&quot;?} answerID 1 correctAnswerID 1 answer Believer expDate
113963500
Yup, and then you're recreating the relational data in Tcl.

NB. XML is not the problem here - you may as well have just used CSV
rather than XML.

A SAX-style interface is problem the easiest way to get what you want.
Here's the solution:

package require xml

namespace eval xml2list {
namespace export convert
variable accumulator
}

proc xml2list::convert xml {
variable accumulator

# we only need to know about the start of elements,
# so create a parser and set the start-element callback
set parser [xml::parser -elementstartcommand [namespace code Start]]
set accumulator {}

# let 'er rip
$parser parse $xml ;# not handling errors

# the result will be in the accumulator
return $accumulator
}

# This procedure gets called for every start tag
proc xml2list::Start {tag attlist args} {
variable accumulator

# the parser has already done the work of turning the
# attributes into a list
lappend accumulator $attlist

return {}
}

### end of script

Notes:

1. This solution is single-threaded. It is an exercise for the reader
to have it run multi-threaded.
2. Attributes as Tcl lists is probably not ideal - a dict would be
better. However, in this case lists are necessary to represent the
relational data.

HTHs,
Steve Ball

Nov 26 '06 #18

P: n/a
In article <11**********************@l12g2000cwl.googlegroups .com>,
Steve Ball <St********@explain.com.auwrote:
Nov 27 '06 #19

P: n/a

Cameron Laird wrote:
In article <11**********************@l12g2000cwl.googlegroups .com>,
Steve Ball <St********@explain.com.auwrote:
.
.
.
So, a bunch of <entryelements where the real data is in attributes.
This looks like a dump of a relational database to me.
Into this:

id 1101 triviaID 233 question {Who wrote &quot;Trilogy of
Knowledge&quot;?} answerID 1 correctAnswerID 1 answer Believer expDate
113963500
Yup, and then you're recreating the relational data in Tcl.

NB. XML is not the problem here - you may as well have just used CSV
rather than XML.
I think this part VERY MUCH deserves repetition.
It is indeed ... silly to serialize relational
data into XML on the way to Tcl.
.
.
.
Notes:

1. This solution is single-threaded. It is an exercise for the reader
to have it run multi-threaded.
2. Attributes as Tcl lists is probably not ideal - a dict would be
better. However, in this case lists are necessary to represent the
relational data.
I'm all in favor of dicts.
Dicts? What is a dict? Again I don't know your terminology

BTW I use XML and not CSV because I would have to rewrite too much of
the site to handle CSV instead of XML, and BTW did I mention that I
have no idea how to handle CSV via TCL in the first place, much less
XML?

I think that you may have to understand that I am not exactly on the
same learning curve with the rest of comp.lang.tcl, apparently a gaggle
of very seasoned engineers with multiple TCL experience, and I'm this
web guy that has ADD. I'm learning as best as I can, but Cameron,
please, you're making me drink from a firehose here!

Oh and BTW this is not a database relational dump. There is no
database; the server that hosts that site has no database and no
business plans to ever implement one.

Phil
>
I don't understand the comment about threading.
How is concurrency an issue at all?
.
.
.
Nov 27 '06 #20

P: n/a
comp.lang.tcl wrote:
>
BTW I use XML and not CSV because I would have to rewrite too much of
the site to handle CSV instead of XML, and BTW did I mention that I
have no idea how to handle CSV via TCL in the first place, much less
XML?
The one thing CSV has over XML is a greatly (*greatly*) reduced learning
curve, and a readily available pure tcl tool for parsing it. (CSV stands
for Comma Separated Values and is just like what it sounds)
I think that you may have to understand that I am not exactly on the
same learning curve with the rest of comp.lang.tcl, apparently a gaggle
of very seasoned engineers with multiple TCL experience, and I'm this
web guy that has ADD. I'm learning as best as I can, but Cameron,
please, you're making me drink from a firehose here!
Consider CSV to be a dixie-cup compared to the firehose that is XML.

For that matter, though, if you control the data you might want to
consider storing the data as a Tcl list in the first place. Then you
don't have to parse it at all.
Nov 27 '06 #21

P: n/a
In article <11**********************@l12g2000cwl.googlegroups .com>,
Steve Ball <St********@explain.com.auwrote:
Nov 27 '06 #22

P: n/a

Bryan Oakley wrote:
comp.lang.tcl wrote:

BTW I use XML and not CSV because I would have to rewrite too much of
the site to handle CSV instead of XML, and BTW did I mention that I
have no idea how to handle CSV via TCL in the first place, much less
XML?

The one thing CSV has over XML is a greatly (*greatly*) reduced learning
curve, and a readily available pure tcl tool for parsing it. (CSV stands
for Comma Separated Values and is just like what it sounds)
I think that you may have to understand that I am not exactly on the
same learning curve with the rest of comp.lang.tcl, apparently a gaggle
of very seasoned engineers with multiple TCL experience, and I'm this
web guy that has ADD. I'm learning as best as I can, but Cameron,
please, you're making me drink from a firehose here!

Consider CSV to be a dixie-cup compared to the firehose that is XML.

For that matter, though, if you control the data you might want to
consider storing the data as a Tcl list in the first place. Then you
don't have to parse it at all.
I'm not sure how you can store a TCL list as a flat file, please show
me how that would be done.

I'm beginning to think I should stop posting here only because I'm
feeling more and more comparatively dumb here :(

Phil

Nov 27 '06 #23

P: n/a
In article <11**********************@f16g2000cwb.googlegroups .com>,
comp.lang.tcl <ph**************@gmail.comwrote:
Nov 27 '06 #24

P: n/a
In article <11**********************@l12g2000cwl.googlegroups .com>,
comp.lang.tcl <ph**************@gmail.comwrote:
>
Cameron Laird wrote:
>In article <tt*****************@newssvr21.news.prodigy.net> ,
Bryan Oakley <oa****@bardo.clearlight.comwrote:
.
.
.
# add a trailing </triviawhich is missing from
# the sample data
set data "$data</trivia>"
.
.
.
Alternatively, as Bryan knows, but doubtless eschewed for
pedagogic reasons,

append data </trivia>

Why? The XML files are not being auto-spawned by TCL or anything else!
I don't understand your question.

Bryan's recommendation included the line

set data "$data</trivia>"

I suspect some readers will want to know that

append data </trivia>

is valid Tcl which produces almost exactly the
same result.
Nov 27 '06 #25

P: n/a
comp.lang.tcl wrote:
Bryan Oakley wrote:
>comp.lang.tcl wrote:
>>BTW I use XML and not CSV because I would have to rewrite too much of
the site to handle CSV instead of XML, and BTW did I mention that I
have no idea how to handle CSV via TCL in the first place, much less
XML?
The one thing CSV has over XML is a greatly (*greatly*) reduced learning
curve, and a readily available pure tcl tool for parsing it. (CSV stands
for Comma Separated Values and is just like what it sounds)
>>I think that you may have to understand that I am not exactly on the
same learning curve with the rest of comp.lang.tcl, apparently a gaggle
of very seasoned engineers with multiple TCL experience, and I'm this
web guy that has ADD. I'm learning as best as I can, but Cameron,
please, you're making me drink from a firehose here!
Consider CSV to be a dixie-cup compared to the firehose that is XML.

For that matter, though, if you control the data you might want to
consider storing the data as a Tcl list in the first place. Then you
don't have to parse it at all.

I'm not sure how you can store a TCL list as a flat file, please show
me how that would be done.
There's no magic. Remember, a tcl list can be represented a string, so
you save it to a file the way you would any string:

set f [open somefile w]
puts $f $tclList
close $f

That puts a tcl list on disk. You can then read it back with this:

set f [open somefile r]
set tclList [read $f]
close $f

What is in $tclList after reading the file is a string representation of
the list, which can be treated as a bona fide list.

Note, you should *not* do this if other processes or people can edit the
file. If there's any chance something could alter the data by, say,
removing or adding curly braces or quotes where they don't belong, you
can't assume what you read in will be a valid list. This technique only
works when you have complete control over the data.
Nov 27 '06 #26

P: n/a
In article <11*********************@l12g2000cwl.googlegroups. com>,
comp.lang.tcl <ph**************@gmail.comwrote:
Nov 28 '06 #27

P: n/a

Bryan Oakley wrote:
comp.lang.tcl wrote:
Bryan Oakley wrote:
comp.lang.tcl wrote:
BTW I use XML and not CSV because I would have to rewrite too much of
the site to handle CSV instead of XML, and BTW did I mention that I
have no idea how to handle CSV via TCL in the first place, much less
XML?
The one thing CSV has over XML is a greatly (*greatly*) reduced learning
curve, and a readily available pure tcl tool for parsing it. (CSV stands
for Comma Separated Values and is just like what it sounds)

I think that you may have to understand that I am not exactly on the
same learning curve with the rest of comp.lang.tcl, apparently a gaggle
of very seasoned engineers with multiple TCL experience, and I'm this
web guy that has ADD. I'm learning as best as I can, but Cameron,
please, you're making me drink from a firehose here!
Consider CSV to be a dixie-cup compared to the firehose that is XML.

For that matter, though, if you control the data you might want to
consider storing the data as a Tcl list in the first place. Then you
don't have to parse it at all.
I'm not sure how you can store a TCL list as a flat file, please show
me how that would be done.

There's no magic. Remember, a tcl list can be represented a string, so
you save it to a file the way you would any string:

set f [open somefile w]
puts $f $tclList
close $f

That puts a tcl list on disk. You can then read it back with this:

set f [open somefile r]
set tclList [read $f]
close $f

What is in $tclList after reading the file is a string representation of
the list, which can be treated as a bona fide list.

Note, you should *not* do this if other processes or people can edit the
file. If there's any chance something could alter the data by, say,
removing or adding curly braces or quotes where they don't belong, you
can't assume what you read in will be a valid list. This technique only
works when you have complete control over the data.
Thanx, going forward I will try to remember that. I appreciate
everyone's help (and patience), looks like I got most of the site
fixed, except for one part that I converted to PHP from Tcl (sorry
guys)

Phil

Nov 28 '06 #28

This discussion thread is closed

Replies have been disabled for this discussion.