From: Richard Owlett on
I'm only 2/3's the glutton for punishment that subject line
implies. My learning style is "hands on". If not efficient, it is
comfortable.

Two questions:
1. Anyone know of a tDOM and/or XML tutorial which is more
focused on reading/parsing an XML file than its creation. My
problem is simplified somewhat as I'm know the files are well formed.

2. I've found that the man pages are rather barren of sample
code. Its sorta of necessity considering intended audience. I was
thinking of something similar to the wiki or the cookbooks hosted
by ActiveState. There would be one "page" of code for each
manpage and each command and major option would be illustrated. I
suspect by the time I've figured out tDOM I will have created one
for that subject. Has someone tried it before?
From: tom.rmadilo on
On Feb 4, 10:32 am, Richard Owlett <rowl...(a)pcnetinc.com> wrote:
> I'm only 2/3's the glutton for punishment that subject line
> implies. My learning style is "hands on". If not efficient, it is
> comfortable.
>
> Two questions:
> 1. Anyone know of a tDOM and/or XML tutorial which is more
> focused on reading/parsing an XML file than its creation. My
> problem is simplified somewhat as I'm know the files are well formed.
>
> 2. I've found that the man pages are rather barren of sample
> code. Its sorta of necessity considering intended audience. I was
> thinking of something similar to the wiki or the cookbooks hosted
> by ActiveState. There would be one "page" of code for each
> manpage and each command and major option would be illustrated. I
> suspect by the time I've figured out tDOM I will have created one
> for that subject. Has someone tried it before?

Here's one example of how I use tDOM to parse a document and then
extract all the data into a Tcl namespace:

proc ::wsclient::getWSDL {
service
url
} {

if {[string match "https://*" "$url"]} {
set result [ns_httpsget $url]
} else {
set result [ns_httpget $url]
}

# to dom doc
dom parse $result wsdlDoc
$wsdlDoc documentElement wsdlRoot

set wsdlNS ::wsclient::${service}::wsdl
namespace eval $wsdlNS { }
namespace eval ::wsclient::${service} [list variable definitions
$result]
::xml::instance::newXMLNS $wsdlNS [$wsdlRoot asList] "1"

rename $wsdlDoc ""

set ::wsclient::${service}::wsdlURL $url

return $wsdlNS
}

As you can see, the parsing only requires one command [dom parse
$result wsdlDoc], which produces the command $wsdlDoc, but the command
also implicitly "validates" the document is at least syntax valid.

From there you can get the documentElement, which is the root element
for the document.

After that I give up on using tDOM because it lacks introspection
capabilities. I use the asList feature to get a Tcl list, which is
turned into the nested tcl namespace representation.

Finally, I destroy the document and memory resources with [rename
$wsdlDoc ""]. I can still access and introspect the data stored in a
tcl namespace ($wsdlNS).

From: Donal K. Fellows on
On 4 Feb, 18:32, Richard Owlett <rowl...(a)pcnetinc.com> wrote:
> 1. Anyone know of a tDOM and/or XML tutorial which is more
> focused on reading/parsing an XML file than its creation. My
> problem is simplified somewhat as I'm know the files are well formed.

There's not that much to it.

package require tdom
set f [open $inputFile]
set doc [dom parse [read $f]]
close $f

Now, $doc is the document node. Use its 'selectNodes' method (XPath
FTW!) to pick out the interesting bits. For example:

foreach node [$doc selectNodes //foo] {
puts "foo([$node @bar]) -> [$node asText]"
}

Try the above fragments with a document like this:

<flub>
<foo bar="123">abc def</foo>
<foo bar="xyz">45&lt;67&gt;89</foo>
</flub>

Donal.
From: Richard Owlett on
Richard Owlett wrote:
>
> 2. I've found that the man pages are rather barren of sample code. Its
> sorta of necessity considering intended audience. I was thinking of
> something similar to the wiki or the cookbooks hosted by ActiveState.
> There would be one "page" of code for each manpage and each command and
> major option would be illustrated. I suspect by the time I've figured
> out tDOM I will have created one for that subject. Has someone tried it
> before?


I've found something along the lines of what I was looking for.
There are a series of tests documented at
http://www.opensource.apple.com/source/tcl/tcl-87/tcl_ext/tdom/tdom/tests/

As their purpose is to exercise very specific portions of code,
they are short, detail oriented and unambiguous. The format is
not particularly convenient for looking up sample code. *BUT*
that is not its purpose.