[英]Powershell XML parse not working on subnodes
I am tring to parse the following file: 我正在尝试解析以下文件:
<?xml version="1.0" encoding="UTF-8" ?>
<printDrivers>
<printDriver id="XE8550">
<inf>x28550p.inf</inf>
<driverPath>\drivers\XEROX\8550\8550_Driver</driverPath>
<printerPaths>
<printerPath>\\print-man\man-25</printerPath>
</printerPaths>
</printDriver>
<printDriver id="HP4050TN">
<inf>hpbf002i.inf</inf>
<driverPath>\drivers\HP\LaserJet\LaserJet_4050_PCL6</driverPath>
<printerPaths>
<printerPath>\\print-man\man-8</printerPath>
<printerPath>\\print-man\man-14</printerPath>
</printerPaths>
</printDriver>
</printDrivers>
with the following powershell script: 使用以下powershell脚本:
$nodelist = $xd.selectnodes("/printDrivers/printDriver") # XPath is case sensitive
foreach ($printerDriverNode in $nodelist) {
$XMLid = $printerDriverNode.getAttribute("id")
$XMLinf = $printerDriverNode.selectSingleNode("inf").get_innerXml()
$XMLdriverPath = $printerDriverNode.selectSingleNode("driverPath").get_innerXml()
$printerPathNodeList = $printerDriverNode.selectNodes("printerPaths/printerPath")
foreach ($printerPathNode in $printerPathNodeList) {
$XMLprinterPath = $printerPathNode.selectSingleNode("printerPath").get_innerXml()
}
}
It all works fine, except for the "nested" node. 除了“嵌套”节点外,其他所有方法都工作正常。 When I run the script it only gets the first .
当我运行脚本时,它只会得到第一个。 ie it won't get \\print-man\\man-14, only \\print-man\\man-8.
即它不会得到\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\-,\\\\\\到\\\\\\\\\\\\\\\\\\ / \\ /////////////// \\ ////-8 / \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\“打印\\\\\\\\\\\\ man-8”。
How can I get it to bring back all of the nodes? 我如何获得它带回所有节点?
Thanks, 谢谢,
Ben 本
That's expected, you're assigning the same variable ($XMLprinterPath) multiple times, so the second value overrides the first. 可以预期,您要多次分配相同的变量($ XMLprinterPath),因此第二个值将覆盖第一个值。 Did you meant to use that as an array or list?
您是否打算将其用作数组或列表?
foreach ($printerPathNode in $printerPathNodeList) {
$XMLprinterPath = $printerPathNode.selectSingleNode("printerPath").get_innerXml()
}
You're already iterating over the printerPaths/printerPath
nodes here - no need to do another .selectsinglenode
on that <printerPath>
node - just read the .InnerXml
or .InnerText
from that $printerPathNode
- that should give you the values you're looking for 您已经在这里遍历了
printerPaths/printerPath
节点-无需在该<printerPath>
节点上执行另一个.selectsinglenode
只需从该$printerPathNode
读取.InnerXml
或.InnerText
应该可以为您提供所需的值对于
If you have PowerShell V2.0 available, you can use Select-Xml
which generates objects for you on-the-fly: 如果有可用的PowerShell V2.0,则可以使用
Select-Xml
即时为您生成对象:
Select-Xml -Path "test.xml" -Xpath "/printDrivers/printDriver" | ForEach-Object {
$id = $_.Node.id
$inf = $_.Node.inf
$driverPath = $_.Node.driverPath
$_.Node | Select-Xml -Xpath "printerPaths/printerPath" | ForEach-Object {
$printerPath = $_.Node.InnerText
}
}
I am guessing you are trying to flatten the XML into objects 我猜您正在尝试将XML扁平化为对象
$r = $xml.printDrivers.printDriver | %{
New-Object PSObject -Property @{
id = $_.id
inf = $_.inf
driverPath = $_.driverPath
printerPaths = @(&{$_.printerPaths | % {$_.printerPath} })
}
}
$r[1].printerPaths
You already have the node, so just use it :) 您已经有了该节点,因此只需使用它即可:)
$nodelist = $xd.selectnodes("/printDrivers/printDriver") # XPath is case sensitive
foreach ($printerDriverNode in $nodelist) {
$printerPathNodeList = $printerDriverNode.selectNodes("printerPaths/printerPath")
foreach ($printerPathNode in $printerPathNodeList) {
write-host $printerPathNode.get_innerXml()
}
}
Note, that it is possible to access xml like objects. 注意,可以像对象一样访问xml。
$xd.printDrivers.printdriver | % { $_.id; $_.printerPaths.printerPath }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.