Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

97 views
Skip to first unread message

comp.lang.tcl

unread,
Nov 25, 2006, 5:57:31 AM11/25/06
to
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:

[quote]
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
[/quote]

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

Bryan Oakley

unread,
Nov 25, 2006, 10:54:12 AM11/25/06
to
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

comp.lang.tcl

unread,
Nov 25, 2006, 1:11:18 PM11/25/06
to

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

Michael Schlenker

unread,
Nov 25, 2006, 1:27:18 PM11/25/06
to
comp.lang.tcl schrieb:
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

comp.lang.tcl

unread,
Nov 25, 2006, 1:37:47 PM11/25/06
to

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

Bryan Oakley

unread,
Nov 25, 2006, 1:42:57 PM11/25/06
to

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.

comp.lang.tcl

unread,
Nov 25, 2006, 1:53:21 PM11/25/06
to

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

Bryan Oakley

unread,
Nov 25, 2006, 2:04:57 PM11/25/06
to
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
</trivia> tag.

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 </trivia> tag, 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 </trivia> which 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 </trivia> tag). Hopefully, though, it will at
least get you started.

comp.lang.tcl

unread,
Nov 25, 2006, 2:15:12 PM11/25/06
to

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 </trivia> tag, 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 </trivia> tag 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 </trivia> which 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 </trivia> tag). 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

Bryan Oakley

unread,
Nov 25, 2006, 2:31:18 PM11/25/06
to
comp.lang.tcl wrote:
> Bryan Oakley wrote:
>>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]
>

Ok, so what you're saying is you did not execute my example verbatim.
You need to first test with the exact code and data in my post to make
sure it will work for at least some subset of data. I'm not convinced
you did that since your example uses $contents. Maybe you just switched
variable names, but I suspect your $contents doesn't contain the exact
sample data you gave us.

Since I can't duplicate your runtime environment (some sort of web
server, apparently) and I can't duplicate your data, I'm at a loss to help.

My guess is, the value of $contents varies from the sample data in some
significant way. The best way to proceed is to first make sure you can
get working results with the sample data you provided. Then, we can
tackle why your real data gets different results.


comp.lang.tcl

unread,
Nov 25, 2006, 2:40:03 PM11/25/06
to

set contents {<?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>}


regexp {<\?.*?\?>(.*$)} $contents -- contents

puts [xml2list $contents]
return [xml2list $contents]
}
[/TCL]

produces the correct list results, but why? Why is this fails then:

[TCL]
set contents [read $fileID [file size ${fileName}.xml]]; close $fileID

# 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

puts "$contents<P><P>"
puts [xml2list $contents]
return [xml2list $contents]
}

[/TCL]

Which produces:

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)

And never produces $contents whatsoever?

Phil

Darren New

unread,
Nov 25, 2006, 2:45:47 PM11/25/06
to
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."

comp.lang.tcl

unread,
Nov 25, 2006, 2:49:46 PM11/25/06
to
[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 "\\1" contents
puts "$contents<P><P>"
return $contents
}
[/TCL]

Produces

<fortune><saying id="1" author="Phil Powell" phrase="New York is SO
Superior!"></saying><saying id="2" author="Phil Powell" phrase="SUBMIT
STUFF!"></saying><saying id="3" author="Old Screams of Abel Slogan"
phrase="Sanctify the Underground!"></saying><saying id="4" author="Phil
Powell" phrase="I believe animals have rights.. they have a right to be
on my dinner plate"></saying><saying id="5" author="Lars Walker"
phrase="..The minimalist approach is also interesting. Sort of a
Scandinavian Modern web site"></saying><saying id="6" author="Phil
Powell" phrase="Don't forget to sign the guestbook!"></saying><saying
id="7" author="Phil Powell" phrase="Jag vill tillbaka till mitt
hemland!"></saying><saying id="8" author="Phil Powell" phrase="Let us
sit by the bonfire, roasting slabs of meat, telling tales of musical
conquest"></saying><saying id="9" author="Phil Powell" phrase="Want to
know God's wonderous sense of grace? You still exist."></saying><saying
id="10" author="Phil Powell" phrase="Gud Rockar - God
Rocks!"></saying><saying id="11" author="Phil Powell" phrase="I need
CAFFEINE!"></saying><saying id="12" author="Micah 4:6-7 (NKJV)"
phrase="&quot;In that day&quot; says the Lord, &quot;I will assemble
the lame, I will gather the outcast, And those whom I have afflicted; I
will make the lame a remnant, And the outcast a strong nation; So the
Lord will reign over them in Mount Zion From now on, even
forever.&quot;"></saying><saying id="13" author="Edgar Allen Poe"
phrase="We are all insane with long intervals of
sanity..."></saying><saying id="14" author="C. S. Lewis"
phrase="Atheism is simple. Real things are not
simple."></saying><saying id="15" author="Phil Powell"
phrase="Nietzsche denied and mocked God, then died insane..
hmm.."></saying><saying id="16" author="Keltin Alexander" phrase="How
can you save those in darkness?
You must be unafraid to enter the darkness, and let your light shine
forth to every corner.
"></saying><saying id="17" author="Phil in reply to
&quot;satanists&quot;" phrase="&quot;..the serpent was created by a god
that doesn't exist..&quot;.. Oh, so Satan doesn't exist
either??!!"></saying><saying id="18" author="Matt's interpretation of
&quot;Screams of Abel&quot;" phrase="Cain, no! Noo no please no! that's
a really big rock Cain, nooooo, Mom won't like this a bit! owww Cain,
now stop that! ouch! you hit like a girl, put that rock down right now
little brother! owww"></saying><saying id="19" author="overheard in
chatroom" phrase="5 out of 10 cannibals agree that vegetarians taste
better.."></saying><saying id="20" author="My London Family"
phrase="&quot;Thank you, please!&quot;"></saying><saying id="21"
author="St.Augustine (354-430 AD)" phrase="In Essentials, unity. In
non-essentials, liberty. In all things, love"></saying><saying id="22"
author="On t-shirts in Norway" phrase="I wear black until they invent a
darker colour!!"></saying><saying id="23" author="from &quot;Dune&quot;
(1985)" phrase="One cannot go against the Word of
God"></saying></fortune>

Now my site won't load at all!! http://valsignalandet.com

Bryan, what do I do?

Phil

comp.lang.tcl

unread,
Nov 25, 2006, 2:50:55 PM11/25/06
to

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

Cameron Laird

unread,
Nov 25, 2006, 2:40:41 PM11/25/06
to
In article <tt0ah.6413$yf7....@newssvr21.news.prodigy.net>,
Bryan Oakley <oak...@bardo.clearlight.com> wrote:
.
.
.

> # add a trailing </trivia> which is missing from
> # the sample data
> set data "$data</trivia>"
.
.
.
Alternatively, as Bryan knows, but doubtless eschewed for
pedagogic reasons,

append data </trivia>

comp.lang.tcl

unread,
Nov 25, 2006, 3:08:09 PM11/25/06
to

Why? The XML files are not being auto-spawned by TCL or anything else!

Bryan Oakley

unread,
Nov 25, 2006, 3:15:27 PM11/25/06
to
comp.lang.tcl wrote:
> ...

> produces the correct list results, but why? Why is this fails then:

The answer to "why" is something along the lines of "because the live
data is different from the sample data" and/or "xml2list wasn't designed
to work with XML in your specific format", and it seems to have
something to do with quotations.

Remember: xml2list is just a simple hack. It's not actually parsing the
xml, it's just making a weak attempt at converting it to a list by
replacing <foo with "{foo" and > with "} ", or something to that effect.
It is very similar to your original program -- it does some simple text
transforms then assumes the result is a list. If there is data it's not
expecting it all falls down like a house of cards.

comp.lang.tcl

unread,
Nov 25, 2006, 3:22:00 PM11/25/06
to

I've given up on xml2list, I don't think it'll work with the XML data I
have, I'm sorry. I don't honestly know what else to do at this point,
but the fact is that the site is breaking and going into now infinite
loops (perhaps segfaults) as a result of my inability to do the very
simple task of converting XML into a TCL list.

Phil

Gerald W. Lester

unread,
Nov 25, 2006, 3:34:00 PM11/25/06
to
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|
+------------------------------------------------------------------------+

comp.lang.tcl

unread,
Nov 25, 2006, 6:39:31 PM11/25/06
to

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

comp.lang.tcl

unread,
Nov 25, 2006, 6:48:57 PM11/25/06
to
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

Mark Janssen

unread,
Nov 25, 2006, 6:56:12 PM11/25/06
to

On Nov 26, 12:39 am, "comp.lang.tcl" <phillip.s.pow...@gmail.com>
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 tdomSorry 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
>

You downloaded the source version and are not allowed to compile it.
Try this instead.:

1) Download http://marknet.tk/downloads/tdom.zip
2) unzip the zip in /home/ppowell/web/cgi-bin/tDom-0.8.0 the zip
contains a binary for tdom for linux
3) Try your script again

Hope that works. I couldn't test it (I am on windows)

Mark

comp.lang.tcl

unread,
Nov 25, 2006, 6:58:11 PM11/25/06
to

I may never find out, /cgi-bin was completely wiped out as a result.

Phil

>
> Mark

comp.lang.tcl

unread,
Nov 25, 2006, 7:21:55 PM11/25/06
to

Ok took 20 mins but the admin was able to restore a backup of /cgi-bin

Also he found out that tDOM is incompatible with the system on
www.clearlight.com so I can't use it in any way shape or form

So back to the drawing board :(

Phil
>
> Phil
>
> >
> > Mark

Gerald W. Lester

unread,
Nov 25, 2006, 8:11:50 PM11/25/06
to

He is on FreeBSD, he needs a build for that OS not linux.

>>> 3) Try your script again
>>>
>>> Hope that works. I couldn't test it (I am on windows)
>> I may never find out, /cgi-bin was completely wiped out as a result.
>
> Ok took 20 mins but the admin was able to restore a backup of /cgi-bin
>
> Also he found out that tDOM is incompatible with the system on
> www.clearlight.com so I can't use it in any way shape or form
>
> So back to the drawing board :(

No, it is not incompatible -- just the build you were directed to is built
for a different OS.

See http://www.tdom.org/#SECTid80ac508

Download the kit (you may be able to source it in, if not unpack it on any
box using SDX and put the freebsd version on your webserver).


>
> Phil
>> Phil
>>
>>> Mark

Mark Janssen

unread,
Nov 25, 2006, 8:22:11 PM11/25/06
to

> >>> 1) Downloadhttp://marknet.tk/downloads/tdom.zip


> >>> 2) unzip the zip in /home/ppowell/web/cgi-bin/tDom-0.8.0 the zip

> >>> contains a binary for tdom for linuxHe is on FreeBSD, he needs a build for that OS not linux.


>
> >>> 3) Try your script again
>
> >>> Hope that works. I couldn't test it (I am on windows)
> >> I may never find out, /cgi-bin was completely wiped out as a result.
>
> > Ok took 20 mins but the admin was able to restore a backup of /cgi-bin
>
> > Also he found out that tDOM is incompatible with the system on

> >www.clearlight.comso I can't use it in any way shape or form
>
> > So back to the drawing board :(No, it is not incompatible -- just the build you were directed to is built
> for a different OS.
>
> Seehttp://www.tdom.org/#SECTid80ac508


>
> Download the kit (you may be able to source it in, if not unpack it on any
> box using SDX and put the freebsd version on your webserver).
>
>

Which was actually the kit I unpacked with SDX and put in a zip-file to
make it easier to install in the parallel thread and that didn't seem
to work.

Mark

comp.lang.tcl

unread,
Nov 25, 2006, 8:30:07 PM11/25/06
to


That was the exact build I downloaded and unzipped onto my directory at
www.clearlight.com and the admin told me it was not compatible. I was
unable to configure it, neither was he, it won't install beyond just
sitting in the directory and that's it.

Phil
>
> Mark

comp.lang.tcl

unread,
Nov 25, 2006, 8:54:26 PM11/25/06
to

**update**

OK I'm sorry I downloaded the wrong package, I got your tdom.zip,
unzipped it and put it on /home/ppowell/web/cgi-bin

added this to xml_procs.tcl

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

Here are the results:

/usr/lib/dld.sl: Can't open shared library:
/home/ppowell/web/cgi-bin/HP-UX-9000/785/tdom.sl /usr/lib/dld.sl: No
such file or directory /usr/lib/dld.sl: Can't open shared library:
/home/ppowell/web/cgi-bin/HP-UX-9000/785/tdom.sl /usr/lib/dld.sl: No
such file or directory error reading package index file
/home/ppowell/web/cgi-bin/pkgIndex.tcl: couldn't load file
"/home/ppowell/web/cgi-bin/HP-UX-9000/785/tdom.sl": no such file or
directory /usr/lib/dld.sl: Can't open shared library:
/home/ppowell/web/cgi-bin/HP-UX-9000/785/tdom.sl /usr/lib/dld.sl: No
such file or directory /usr/lib/dld.sl: Can't open shared library:
/home/ppowell/web/cgi-bin/HP-UX-9000/785/tdom.sl /usr/lib/dld.sl: No
such file or directory couldn't load file
"/home/ppowell/web/cgi-bin/HP-UX-9000/785/tdom.sl": no such file or
directory while executing "load "[file join $dir [platform] $lib]""
(procedure "loadlib" line 6) invoked from within "loadlib
/home/ppowell/web/cgi-bin tdom 0.7.8" ("package ifneeded" script)

Mark Janssen

unread,
Nov 25, 2006, 9:14:58 PM11/25/06
to

> > > >www.clearlight.comsoI can't use it in any way shape or form


>
> > > > So back to the drawing board :(No, it is not incompatible -- just the build you were directed to is built
> > > for a different OS.
>
> > > Seehttp://www.tdom.org/#SECTid80ac508
>
> > > Download the kit (you may be able to source it in, if not unpack it on any
> > > box using SDX and put the freebsd version on your webserver).
>
> > Which was actually the kit I unpacked with SDX and put in a zip-file to
> > make it easier to install in the parallel thread and that didn't seem
> > to work.
>
> > Mark**update**
>
> OK I'm sorry I downloaded the wrong package, I got your tdom.zip,
> unzipped it and put it on /home/ppowell/web/cgi-bin
>
> added this to xml_procs.tcl
>
> lappend auto_path /home/ppowell/web/cgi-bin
> package require tdom
>
> Here are the results:
>
> /usr/lib/dld.sl: Can't open shared library:
> /home/ppowell/web/cgi-bin/HP-UX-9000/785/tdom.sl /usr/lib/dld.sl: No
> such file or directory /usr/lib/dld.sl: Can't open shared library:

So it seems your website is hosted on a HP-UX platform, which the zip
file (as you already discovered) does not contain binaries for. Because
you cannot compile on that machine and I don't have HP-UX available, I
cannot see a way forward in this direction.
I think for your platform, the more promising possibility will then be
to use a Tcl only solution (either TclXML or the proposals made here
and in the "How can I ensure I always have a list thread")

Good luck,
Mark

Gerald W. Lester

unread,
Nov 25, 2006, 9:57:29 PM11/25/06
to
Mark Janssen wrote:
>
>> /usr/lib/dld.sl: Can't open shared library:
>> /home/ppowell/web/cgi-bin/HP-UX-9000/785/tdom.sl /usr/lib/dld.sl: No
>> such file or directory /usr/lib/dld.sl: Can't open shared library:
>
> So it seems your website is hosted on a HP-UX platform, which the zip
> file (as you already discovered) does not contain binaries for. Because
> you cannot compile on that machine and I don't have HP-UX available, I
> cannot see a way forward in this direction.
> I think for your platform, the more promising possibility will then be
> to use a Tcl only solution (either TclXML or the proposals made here
> and in the "How can I ensure I always have a list thread")

ActiveState's (www.activestate.com) distro supports HP-UX (which is *not*
the same as FreeBSD) and has tdom in it.

Download it and at least put the tdom piece on your site.

comp.lang.tcl

unread,
Nov 25, 2006, 10:08:10 PM11/25/06
to

Gerald W. Lester wrote:
> Mark Janssen wrote:
> >
> >> /usr/lib/dld.sl: Can't open shared library:
> >> /home/ppowell/web/cgi-bin/HP-UX-9000/785/tdom.sl /usr/lib/dld.sl: No
> >> such file or directory /usr/lib/dld.sl: Can't open shared library:
> >
> > So it seems your website is hosted on a HP-UX platform, which the zip
> > file (as you already discovered) does not contain binaries for. Because
> > you cannot compile on that machine and I don't have HP-UX available, I
> > cannot see a way forward in this direction.
> > I think for your platform, the more promising possibility will then be
> > to use a Tcl only solution (either TclXML or the proposals made here
> > and in the "How can I ensure I always have a list thread")
>
> ActiveState's (www.activestate.com) distro supports HP-UX (which is *not*
> the same as FreeBSD) and has tdom in it.
>
> Download it and at least put the tdom piece on your site.

Sorry but there is no budget for that one :( I was hoping for a more
public-domain download than this, anything out there?

Phil

Gerald W. Lester

unread,
Nov 25, 2006, 10:22:56 PM11/25/06
to
comp.lang.tcl wrote:
> Gerald W. Lester wrote:
>> Mark Janssen wrote:
>>>> /usr/lib/dld.sl: Can't open shared library:
>>>> /home/ppowell/web/cgi-bin/HP-UX-9000/785/tdom.sl /usr/lib/dld.sl: No
>>>> such file or directory /usr/lib/dld.sl: Can't open shared library:
>>> So it seems your website is hosted on a HP-UX platform, which the zip
>>> file (as you already discovered) does not contain binaries for. Because
>>> you cannot compile on that machine and I don't have HP-UX available, I
>>> cannot see a way forward in this direction.
>>> I think for your platform, the more promising possibility will then be
>>> to use a Tcl only solution (either TclXML or the proposals made here
>>> and in the "How can I ensure I always have a list thread")
>> ActiveState's (www.activestate.com) distro supports HP-UX (which is *not*
>> the same as FreeBSD) and has tdom in it.
>>
>> Download it and at least put the tdom piece on your site.
>
> Sorry but there is no budget for that one :( I was hoping for a more
> public-domain download than this, anything out there?

It is free as long as you do not ship it.

comp.lang.tcl

unread,
Nov 25, 2006, 11:06:14 PM11/25/06
to

Gerald W. Lester wrote:
> comp.lang.tcl wrote:
> > Gerald W. Lester wrote:
> >> Mark Janssen wrote:
> >>>> /usr/lib/dld.sl: Can't open shared library:
> >>>> /home/ppowell/web/cgi-bin/HP-UX-9000/785/tdom.sl /usr/lib/dld.sl: No
> >>>> such file or directory /usr/lib/dld.sl: Can't open shared library:
> >>> So it seems your website is hosted on a HP-UX platform, which the zip
> >>> file (as you already discovered) does not contain binaries for. Because
> >>> you cannot compile on that machine and I don't have HP-UX available, I
> >>> cannot see a way forward in this direction.
> >>> I think for your platform, the more promising possibility will then be
> >>> to use a Tcl only solution (either TclXML or the proposals made here
> >>> and in the "How can I ensure I always have a list thread")
> >> ActiveState's (www.activestate.com) distro supports HP-UX (which is *not*
> >> the same as FreeBSD) and has tdom in it.
> >>
> >> Download it and at least put the tdom piece on your site.
> >
> > Sorry but there is no budget for that one :( I was hoping for a more
> > public-domain download than this, anything out there?
>
> It is free as long as you do not ship it.

Sorry, stupid moment.. I downloaded it, unzipped it, but what exactly
do I move over to the HP-UX server? Do I just move tdom.tcl or do I
move more than that?

I am not able to install the entire ActiveState suite, we're not
alloted that much room, sorry

Phil

Gerald W. Lester

unread,
Nov 25, 2006, 11:18:29 PM11/25/06
to

Not sure on HP-UX where everything is, but on Windows it is all in the
tdom0.8.1 directory.

It should be at least: pkgIndex.tcl, tdom.tcl, tdom081.so (although this one
may have some other letters in its name).

Ramon Ribó

unread,
Nov 26, 2006, 5:30:57 AM11/26/06
to

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
<phillip....@gmail.com> escribió:
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:

Steve Ball

unread,
Nov 26, 2006, 4:04:23 PM11/26/06
to
> 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 <entry> elements 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

comp.lang.tcl

unread,
Nov 27, 2006, 11:54:10 AM11/27/06
to

I just plain didn't know what to do so I probably overdid it, did a scp
-pr of the entire tdom0.8.1 directory for HP-UX over to the remote site
under /cgi-bin, and when I put this on the top of xml_procs.tcl, it did
not produce any kind of syntax or evaluation error:

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

Now, going to http://wiki.tcl.tk/8984 to try to understand tdom, I have
no idea what this site is talking about. All I want to do is take
every attribute key => val pair and convert it to a well-ordered TCL
list, and this site does not explain it to me in such a way that I'd
understand.

Thanx though so far, sorry I'm so nearly impossible to handle!

Phil

suchenwi

unread,
Nov 27, 2006, 12:16:51 PM11/27/06
to
OK, here's a hopefully real simple example with tdom (done in an
interactive tclsh):

#------------------------- create an example XML buffer
% set xml {<foo><bar a="1" b="2" c="3"/><bar a="uno" b="dos" c="tres"
/></foo>}
<foo><bar a="1" b="2" c="3"/><bar a="uno" b="dos" c="tres" /></foo>

#----------------------------- make sure we get tdom
% package require tdom
080

#------------------------------ parse our buffer, get a handle to its
root node
% set root [[dom parse $xml] documentElement]
domNode00BE1140

#----------------------------- dump the root (and everything below it)
as a list of lists
% $root asList
foo {} {{bar {a 1 b 2 c 3} {}} {bar {a uno b dos c tres} {}}}

#------------------------------ walk over all the attribute contents:
% foreach i [lindex [$root asList] 2] {puts item=[lindex $i 1]}
item=a 1 b 2 c 3
item=a uno b dos c tres

comp.lang.tcl

unread,
Nov 27, 2006, 1:07:05 PM11/27/06
to

suchenwi wrote:
> OK, here's a hopefully real simple example with tdom (done in an
> interactive tclsh):
>
> #------------------------- create an example XML buffer
> % set xml {<foo><bar a="1" b="2" c="3"/><bar a="uno" b="dos" c="tres"
> /></foo>}
> <foo><bar a="1" b="2" c="3"/><bar a="uno" b="dos" c="tres" /></foo>
>
> #----------------------------- make sure we get tdom
> % package require tdom
> 080
>
> #------------------------------ parse our buffer, get a handle to its
> root node
> % set root [[dom parse $xml] documentElement]
> domNode00BE1140

Ok, break it down for me, what's a node? I honestly don't know.

>
> #----------------------------- dump the root (and everything below it)
> as a list of lists
> % $root asList
> foo {} {{bar {a 1 b 2 c 3} {}} {bar {a uno b dos c tres} {}}}
>
> #------------------------------ walk over all the attribute contents:
> % foreach i [lindex [$root asList] 2] {puts item=[lindex $i 1]}
> item=a 1 b 2 c 3
> item=a uno b dos c tres

ah cool thanx I will try that!
Phil

Cameron Laird

unread,
Nov 27, 2006, 4:04:02 PM11/27/06
to
In article <1164575063.7...@l12g2000cwl.googlegroups.com>,
Steve Ball <Steve...@explain.com.au> wrote:
.
.

.
>So, a bunch of <entry> elements 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.

I don't understand the comment about threading.
How is concurrency an issue at all?
.
.
.

comp.lang.tcl

unread,
Nov 27, 2006, 4:38:35 PM11/27/06
to

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

Bryan Oakley

unread,
Nov 27, 2006, 4:48:33 PM11/27/06
to
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.

Cameron Laird

unread,
Nov 27, 2006, 4:32:02 PM11/27/06
to
>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
.
.
.
Phil says <URL:
http://groups.google.com/group/comp.lang.tcl/msg/6b0f6e6f9cf41e39 >
that, were this PHP, he'd write

$parser = @xml_parser_create();
@xml_parser_set_options($parser, XML_OPTION_SKIP_WHITE, true);
@xml_parse_into_struct($parser, $xml, $xmlArray, $tags);
@xml_parser_free($parser);

This certainly compares favorably in concision to Steve's solution.

It's comparison that deserves a bit more explanation, though. First,
unless Phil is running a non-standard installation, the second line
needs to be

@xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, true);

Also, the result puts essentially an entire DOM tree in $xmlArray;
my guess is that Phil assumes dereferencing of the [attributes] of
the [VIA] node(s). The PHP program above, though, does NOT make a
list of all attributes of all tags, as Steve and others of us read
the problem.

I don't have any particular conclusion. For those thinking DOM
and Tcl and SAX are complicated, I recommend study of <URL:
http://www.php.net/manual/en/function.xml-parse-into-struct.php >:
"This function parses an XML file into 2 parallel array structures
..." In any case, the simple PHP script gives the promised results
only with extra assumptions.

comp.lang.tcl

unread,
Nov 27, 2006, 5:59:23 PM11/27/06
to

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

Cameron Laird

unread,
Nov 27, 2006, 5:32:42 PM11/27/06
to
In article <1164663514.8...@f16g2000cwb.googlegroups.com>,
comp.lang.tcl <phillip....@gmail.com> wrote:
.
.

.
>Dicts? What is a dict? Again I don't know your terminology
.
.
.

>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.
.
.
.
XML's fine by me.

What's left to do? Michael, Bryan, and Steve have all given
what look to me to be quite nice solutions to the problem
you expressed, and I just posted what I believe is the
shortest one yet. Is there any barrier still standing between
you and the XML parsing you're after?

I'm sympathetic to cultural differences. I find it very
frustrating to read the abbreviations that are common with an
in-group. The clt community is NOT limited to "very
seasoned engineers", though, I can assure you. From all I can
tell, we're not the ones spraying a firehose; we keep trying
to find one specific symptom on which we can focus.

If you can [package require tdom], you're only a few lines
away from the solution you have written you're after.

I'm narrowing follow-ups.

Cameron Laird

unread,
Nov 27, 2006, 5:37:48 PM11/27/06
to
In article <1164485289.6...@l12g2000cwl.googlegroups.com>,

comp.lang.tcl <phillip....@gmail.com> wrote:
>
>Cameron Laird wrote:
>> In article <tt0ah.6413$yf7....@newssvr21.news.prodigy.net>,
>> Bryan Oakley <oak...@bardo.clearlight.com> wrote:
>> .
>> .
>> .
>> > # add a trailing </trivia> which 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.

Cameron Laird

unread,
Nov 27, 2006, 5:40:28 PM11/27/06
to
In article <1164500515.1...@f16g2000cwb.googlegroups.com>,

comp.lang.tcl <phillip....@gmail.com> wrote:
.
.
.
>Ok took 20 mins but the admin was able to restore a backup of /cgi-bin
>
>Also he found out that tDOM is incompatible with the system on
>www.clearlight.com so I can't use it in any way shape or form
.
.
.
You can achieve the result you've written without tDOM.
I'd be curious to learn, though, how tDOM is incompatible
with www.clearlight.com. Maybe there's a misunderstanding,
and tDOM is more available to you than you realize.

Bryan Oakley

unread,
Nov 27, 2006, 6:21:42 PM11/27/06
to

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.


Cameron Laird

unread,
Nov 27, 2006, 6:44:53 PM11/27/06
to
In article <1164668363....@l12g2000cwl.googlegroups.com>,

comp.lang.tcl <phillip....@gmail.com> wrote:
.
.
.
>I'm not sure how you can store a TCL list as a flat file, please show
>me how that would be done.
.
.
.
<URL: http://wiki.tcl.tk/15296 >

comp.lang.tcl

unread,
Nov 28, 2006, 2:35:57 AM11/28/06
to

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

0 new messages