2

In TCL Script

<server serverName='localhost'>
    <port Number="6209" xmlLoc="path">
        <pollerName>Nymex Feed Test1</pollerName>
        <pollerName>Nymex Feed Test2</pollerName>
    </port>
    <port Number="6209" xmlLoc="path">
        <pollerName>Nymex Feed Test1</pollerName>
        <pollerName>Nymex Feed Test2</pollerName>
    </port>
</server>

I want to read above xml tag attributes and node values into variables.

Please advise.

Updated: Below is the final Solution to read XML.

package require tdom

set xmlpath "test.xml"
set fd [open $xmlpath]
set xml [read $fd]
set doc [dom parse $xml]
set root [$doc documentElement]

foreach node [$root selectNodes "/server/port"] {

set num [$node getAttribute Number]
set loc [$node getAttribute xmlLoc]

puts $num
puts $loc

foreach nodeList [$node selectNodes  ./pollerName/text()] {
puts [$nodeList nodeValue]
}

}

Later on:

i can not use variable into nested loop to call Function , it fails to call Function in second loop. Please advise CODE::

proc remoteTelnet {serNumber pNumber xmlRoot} {

        set body1 [concat $body "<tr><td>$pNumber and $polName</td> <td>Password not changed</td></tr>"]
puts "Poller:-- $polName"

}

foreach node [$root selectNodes "/server/port"] {

set num [$node getAttribute Number]
set loc [$node getAttribute xmlLoc]

foreach nodeList [$node selectNodes  ./pollerName/text()] {
set poller [$nodeList nodeValue]

puts "Poller: $poller"
puts "Port: $num"
puts "Xml Path: $loc"
remoteTelnet $host $num $loc

}
}
Hmnshu
  • 230
  • 1
  • 5
  • 13
  • Google gives [TclXML](http://tclxml.sourceforge.net/tclxml.html) and [this wiki](http://wiki.tcl.tk/11020). Did you try any one of those before asking your question here? – Jerry Feb 21 '14 at 09:48
  • I used tdom TCL package, i am not aware fo what you said? – Hmnshu Feb 21 '14 at 09:51

1 Answers1

2

It's pretty easy to get all that stuff with tDOM with XPath.

# Assuming you've already read the data as simple text into a variable
set doc [dom parse $XML]

Now let's get a value out of an attribute:

set firstPortNumber [lindex [$dom selectNodes /server/port/@Number] 0 1]

Great! Now let's get the text content of an element:

set pollerName [[$doc selectNodes {/server/port[1]/pollerName[1]}] asText]

You can do lots of things with XPath plus basic DOM operations. Just be aware that some parts of XPath can interfere with Tcl's syntax; you may need to quote search terms (as above).

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • in case i need to loop into, and there could be more tags then above sample – Hmnshu Feb 21 '14 at 10:03
  • @Hmnshu The `selectNodes` method returns a list of nodes that you can iterate over (except for attributes, apparently, when it returns a list of pairs) so `foreach node [$doc selectNodes …] { dostuff }` is the general pattern. – Donal Fellows Feb 21 '14 at 10:52
  • Hi Donal, i can not use variable into nested loop , it fails to print values in second loop. Please advise CODE:: " foreach node [$root selectNodes "/server/port"] { set num [$node getAttribute Number] set loc [$node getAttribute xmlLoc] foreach nodeList [$node selectNodes ./pollerName/text()] { set poller [$nodeList nodeValue] puts "Poller: $poller" puts "Port: $num" puts "Xml Path: $loc" remoteTelnet $host $num $loc } } " – Hmnshu Feb 24 '14 at 13:52