For Programmers: Free Programming Magazines  


Home > Archive > Tcl > September 2006 > Adding and updating new tDOM XML text nodes









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Adding and updating new tDOM XML text nodes
Synic

2006-09-22, 10:01 pm

Hi guys,

Some code:

package require tdom
# Read in data in the xml variable.
set dom [dom parse -keepEmpties \
"<BASE>\n<al>\n<Cats>9 lives</Cats>\n<Canaries/>\n</al>\n</BASE>"]
set doc [$dom documentElement]
# Add a missing node.
set node [$doc selectNodes /BASE/al]
$node appendXML "<Dogs></Dogs>"
# Update an existing node.
set node [$doc selectNodes /BASE/al/Cats/text()]
$node nodeValue "Great swingers"
# Output doc to show Dogs got added and Cats changed.
puts [$doc asXML]
# Try to update the node with some data.
set node [$doc selectNodes /BASE/al/Dogs/text()]
$node nodeValue "Go Woof"

Output:

<BASE>
<al>
<Cats>Great swingers</Cats>

<Canaries/>

<Dogs/>
</al>

</BASE>

invalid command name ""
while executing
"$node nodeValue "Go Woof""
(file "t" line 15)

Platform:

ActiveTCL 8.4.13.0.261555.

Strangeness:

I can add the new node to the XML (and it's even reformatted) for it to
be called in when doing the $doc asXML, but, I can't update a newly
added node.

Anyone have any suggestions?

Rolf Ade

2006-09-24, 8:03 am

In article <slrneh8g2p.irn.flavp+hfrarg@sparrow.autons.net.au>,
Synic <flavp+hfrarg@nhgbaf.arg.nh> wrote:
>Some code:
>
> package require tdom
> # Read in data in the xml variable.
> set dom [dom parse -keepEmpties \
> "<BASE>\n<al>\n<Cats>9 lives</Cats>\n<Canaries/>\n</al>\n</BASE>"]
> set doc [$dom documentElement]
> # Add a missing node.
> set node [$doc selectNodes /BASE/al]
> $node appendXML "<Dogs></Dogs>"

^^^^
You add a 'Dogs' node without any childs or content.

> # Update an existing node.
> set node [$doc selectNodes /BASE/al/Cats/text()]
> $node nodeValue "Great swingers"
> # Output doc to show Dogs got added and Cats changed.
> puts [$doc asXML]
> # Try to update the node with some data.
> set node [$doc selectNodes /BASE/al/Dogs/text()]


That is your mistake. With your XPath expression, you select all text
node childs of your newly inserted Dogs element. There aren't any.
Therefor, you get an empty list as result. That is confirmend by your
output, see below


> $node nodeValue "Go Woof" Output:
> <BASE>
> <al>
> <Cats>Great swingers</Cats>
>
> <Canaries/>
>
> <Dogs/>


You see: empty 'Dogs' element, no element node childs, no text
content.

> </al>
>
> </BASE>
>
> invalid command name ""
> while executing
> "$node nodeValue "Go Woof""
> (file "t" line 15)
>
>Platform:
>
>ActiveTCL 8.4.13.0.261555.
>
>Strangeness:
>
>I can add the new node to the XML (and it's even reformatted) for it to
>be called in when doing the $doc asXML, but, I can't update a newly
>added node.


You've an at least partly wrong mental model of DOM. From the DOM data
model view, elements and pcdata text are just nodes (of different
types) in the DOM tree. You don't update a newly added node, you
either modify the node value of an already existing text node child of
your element node (if there is already one (btw. there could be more
than one)). Or, as in your case, if your newly added node doesn't have
a text node child, you've to add one. One way to do this in your
example code would be:

set newTextNode [$doc createTextNode "Go Woof"]
set dogsNode [$doc selectNodes /BASE/al/Dogs]
$dogsNode appendChild $newTextNode

hth
rolf
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com