I’m using tDom to loop through some XML and pull out each element’s text().
set xml {
<systems>
<object>
<type>Hardware</type>
<name>Server Name</name>
<attributes>
<vendor></vendor>
</attributes>
</object>
<object>
<type>Hardware</type>
<name>Server Two Name</name>
<attributes>
<vendor></vendor>
</attributes>
</object>
</systems>
};
set doc [dom parse $xml]
set root [$doc documentElement]
set nodeList [$root selectNodes /systems/object]
foreach node $nodeList {
set nType [$node selectNodes type/text()]
set nName [$node selectNodes name/text()]
set nVendor [$node selectNodes attributes/vendor/text()]
# Etc...
puts "Type: "
puts [$nType data]
# Etc ..
puts [$nVendor data]
}
But when it tries to print out the Vendor, which is empty, it thows the error invalid command name “”. How can I ignore this and just set $nVendor to an empty string?
The
selectNodesmethod of a node returns a list of nodes that match your pattern. When you use the results directly as a commandwhat you are really doing is taking advantage of the fact that a list of 1 item (the number of
nameitems) is the same as one item. When there are no matching nodes, you get back an empty listand, when you call that, it’s throwing an error because you’re calling a command with the name
{}.As noted by Hai Vu, you can test that there was a result by checking the result against
"". A “more correct” way would probably be to check it against the empty listor, to be even more complete (if you’re not sure about the input XML)