Access keys in a node once you have the parent node


given xml file

<?xml version="1.0"?>
<locations>
<location id = "austin">
<strings>
<root>"\\ausfs\"</root>
<jobs>"austin jobs"</jobs>
<flexlm_servers>"@dallic01;@atllic01;@tamlic01"</flexlm_servers>
<rs_accel>"ausrevitsvr01"</rs_accel>
</strings>
</location>

<location id = "denver">
<strings>
<root>"\\denfs\"</root>
<jobs>"denver jobs"</jobs>
<flexlm_servers>"@dallic01;@atllic01;@tamlic01"</flexlm_servers>
<rs_accel>"denrevitsvr01"</rs_accel>
</strings>
</location>
</locations>

i need hash table populated key/value pairs strings node, , named particular location.

i can arbitrary location with
$locationsnode = $xml.selectnodes("/locations/location") | {$_.id -eq $location}
where $location particular location looking for. (and mind you, figuring out piping major milestone in "think posh, not vbs" progress ;-)
, can strings node of location with
$stringsnode = $locationsnode.selectnodes("./strings")

but seem unable contents once have appropriate strings node. have tried 
foreach ($string in $stringsnode)
but seems produce strings node agin, don't understand.
also, should note name , number of tags in strings arbitrary, need read them , them hash table.

lastly, curious, difference between defining $xml variable , using load vs get-content approach? have seen both, , not sure when 1 better other.

any appreciated.
gordon

hi gordon,

here's 1 way of doing that.  combined of queries single selectnodes call, example of xpath queries can you.

$xml = [xml](get-content test.xml) $location = 'denver'  $hashtable = @{}  foreach ($node in $xml.selectnodes("/locations/location[@id='$location']/strings/*")) {     $hashtable[$node.name] = $node.'#text' }  $hashtable

regarding question whether cast strings [xml] type, or create xmldocument object , call load(), there's no difference (so long you're reading file.)  if want modify , save file, may find want go load() method, that's way specify options allow preserve whitespace in file.



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

CRL Revocation always failed

Failed to query the results of bpa xpath

0x300000d errors in Microsoft Remote Desktop client