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

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

Post by comp.lang. » Sun, 26 Nov 2006 19:57:31


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("<?xml
version="1.0" 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
 
 
 

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

Post by Bryan Oakl » Mon, 27 Nov 2006 00:54:12


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://www.yqcomputer.com/

 
 
 

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

Post by comp.lang. » Mon, 27 Nov 2006 03:11:18


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
 
 
 

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

Post by Michael Sc » Mon, 27 Nov 2006 03:27:18

omp.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
 
 
 

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

Post by comp.lang. » Mon, 27 Nov 2006 03:37:47

Michael Schlenker wrote:

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 "Trilogy of Knowledge"?"
answerID="1" correctAnswerID="1" answer="Believer"
expDate="1139634000"></entry><entry id="1102" triviaID="233"
question="Who wrote "Trilogy of Knowledge"?" answerID="2"
correctAnswerID="1" answer="Saviour Machine"
expDate="1139634000"></entry><entry id="1103" triviaID="233"
question="Who wrote "Trilogy of Knowledge"?" answerID="3"
correctAnswerID="1" answer="Seventh Avenue"
expDate="1139634000"></entry><entry id="1104" triviaID="233"
question="Who wrote "Trilogy of Knowledge"?" answerID="4"
correctAnswerID="1" answer="Inevitable End"
expDate="1139634000"></entry><entry id="1105" triviaID="233"
question="Who wrote "Trilogy of Knowledge"?" answerID="5"
correctAnswerID="1" answer="No such song existed"
expDate="1139634000"></entry>

Phil



 
 
 

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

Post by Bryan Oakl » Mon, 27 Nov 2006 03:42:57


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://www.yqcomputer.com/ '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.
 
 
 

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

Post by comp.lang. » Mon, 27 Nov 2006 03:53:21

Bryan Oakley wrote:

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)



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 "Trilogy of Knowledge"?"
answerID="1" correctAnswerID="1" answer="Believer"
expDate="1139634000"></entry><entry id="1102" triviaID="233"
question="Who wrote "Trilogy of Knowledge"?" answerID="2"
correctAnswerID="1" answer="Saviour Machine"
expDate="1139634000"></entry><entry id="1103" triviaID="233"
question="Who wrote "Trilogy of Knowledge"?" answerID="3"
correctAnswerID="1" answer="Seventh Avenue"
expDate="1139634000"></entry><entry id="1104" triviaID="233"
question="Who wrote "Trilogy of Knowledge"?" answerID="4"
correctAnswerID="1" answer="Inevitable End"
expDate="1139634000"></entry><entry id="1105" triviaID="233"
question="Who wrote "Trilogy of Knowledge"?" answerID="5"
correctAnswerID="1" answer="No such song existed"
expDate="1139634000"></entry></trivia>

Into this:

id 1101 triviaID 233 question {Who wrote "Trilogy of
Knowledge"?} 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


 
 
 

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

Post by Bryan Oakl » Mon, 27 Nov 2006 04:04:57

omp.lang.tcl wrote:

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 "Trilogy of Knowledge"?"
answerID="1" correctAnswerID="1" answer="Believer"
expDate="1139634000"></entry><entry id="1102" triviaID="233"
question="Who wrote "Trilogy of Knowledge"?" answerID="2"
correctAnswerID="1" answer="Saviour Machine"
expDate="1139634000"></entry><entry id="1103" triviaID="233"
question="Who wrote "Trilogy of Knowledge"?" answerID="3"
correctAnswerID="1" answer="Seventh Avenue"
expDate="1139634000"></entry><entry id="1104" triviaID="233"
question="Who wrote "Trilogy of Knowledge"?" answerID="4"
correctAnswerID="1" answer="Inevitable End"
expDate="1139634000"></entry><entry id="1105" triviaID="233"
question="Who wrote "Trilogy of Knowledge"?" 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
"Trilogy of Knowledge"?} answerID 1 correctAnswerID 1 answer
Believer expDate 1139634000} {}} {entry {id 1102 triviaID 233 question
{Who wrote "Trilogy of Knowledge"?} answerID 2 correctAnswerID
1 answer {Saviour Machine} expDate 1139634000} {}} {entry {id 1103
triviaID 233 question {Who wrote "Trilogy of Knowledge"?}
answerID 3 correctAnswerID 1 answer {Seventh Avenue} expDate 1139634000}
{}} {entry {id 1104 triviaID 233 question {Who wrote "Trilogy of
Knowledge"?} answerID 4 correctAnswerID 1 answer {Inevitable End}
expDate 1139634000} {}} {entry {id 1105 triviaID 233 question {Who wrote
"Trilogy of Knowledge"?} 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 " 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
 
 
 

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

Post by comp.lang. » Mon, 27 Nov 2006 04:15:12

Bryan Oakley wrote:

Ok done



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



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]


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

 
 
 

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

Post by clair » Mon, 27 Nov 2006 04:40:41

In article <tt0ah.6413$ XXXX@XXXXX.COM >,

.
.
.
.
.
.
Alternatively, as Bryan knows, but doubtless eschewed for
pedagogic reasons,

append data </trivia>
 
 
 

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

Post by Darren Ne » Mon, 27 Nov 2006 04:45:47


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."
 
 
 

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

Post by comp.lang. » Mon, 27 Nov 2006 04:50:55


Sorry there is no attachment, no zip file of any kind.

Phil
 
 
 

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

Post by comp.lang. » Mon, 27 Nov 2006 05:08:09


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

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

Post by Gerald W. » Mon, 27 Nov 2006 05:34:00


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 "Trilogy
of Knowledge"?" answerID="1" correctAnswerID="1" answer="Believer"
expDate="1139634000"></entry>
<entry id="1102" triviaID="233" question="Who wrote "Trilogy
of Knowledge"?" answerID="2" correctAnswerID="1" answer="Saviour
Machine" expDate="1139634000"> </entry>
<entry id="1103" triviaID="233" question="Who wrote "Trilogy
of Knowledge"?" answerID="3" correctAnswerID="1" answer="Seventh
Avenue" expDate="1139634000"></entry>
<entry id="1104" triviaID="233" question="Who wrote "Trilogy
of Knowledge"?" answerID="4" correctAnswerID="1" answer="Inevitable
End" expDate="1139634000"></entry>
<entry id="1105" triviaID="233" question="Who wrote "Trilogy
of Knowledge"?" 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|
+------------------------------------------------------------------------+
 
 
 

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

Post by comp.lang. » Mon, 27 Nov 2006 08:39:31

Gerald W. Lester wrote:

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