gpt4 book ai didi

xml - Powershell XML 解析不适​​用于子节点

转载 作者:行者123 更新时间:2023-12-02 07:34:14 25 4
gpt4 key购买 nike

我正在尝试解析以下文件:

<?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>

使用以下 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()
}
}

除了“嵌套”节点之外,一切正常。当我运行脚本时,它只获取第一个 .即它不会得到\print-man\man-14,只会得到\print-man\man-8。

如何让它恢复所有节点?

谢谢

最佳答案

 foreach ($printerPathNode in $printerPathNodeList) {
$XMLprinterPath = $printerPathNode.selectSingleNode("printerPath").get_innerXml()
}

您已经在迭代 printerPaths/printerPath这里的节点 - 无需再做 .selectsinglenode在那<printerPath>节点 - 只需阅读 .InnerXml.InnerText由此$printerPathNode - 这应该会给你你正在寻找的值

关于xml - Powershell XML 解析不适​​用于子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3299227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com