gpt4 book ai didi

xml - 在Powershell中解析XML

转载 作者:行者123 更新时间:2023-12-03 01:11:37 41 4
gpt4 key购买 nike

如果我有一个XML文件,例如:

<?xml version="1.0" encoding="utf-8"?>
<package date-created="19/07/2016" version="6" system="6">
<description />
<system />
<res id="1057002654" name="ABC" path="/DEF" type="F" mimeType="application/x-shared">
<description />
<keywords />
<resver id="1057014163" major="1" minor="0" revision="0" effective-from="20010101000000" effective-to="20991231235959" />
<resver id="1057014677" major="2" minor="0" revision="0" effective-from="20010101000000" effective-to="20991231235959" />
<resver id="1057015106" major="3" minor="0" revision="0" effective-from="20010101000000" effective-to="20991231235959" />
<resver id="1057016183" major="4" minor="0" revision="0" effective-from="20010101000000" effective-to="20991231235959" />
<resver id="1057016374" major="5" minor="0" revision="0" effective-from="20010101000000" effective-to="20991231235959" />
<resver id="1057017622" major="6" minor="0" revision="0" effective-from="20010101000000" effective-to="20991231235959" />
<resver id="1057017704" major="7" minor="0" revision="0" effective-from="20010101000000" effective-to="20991231235959" />
<resver id="1057017863" major="8" minor="0" revision="0" effective-from="20010101000000" effective-to="20991231235959" />
</res>
</package>

可能有数百种物品,其中可能有一个或多个。在Powershell中,我尝试查找最后一个 resver id 值,其中 res id 等于 1057002654

我从这个开始:
$path = 'C:\TEST\package.xml'
$myxml = [xml](Get-Content $path)
$node = $myxml.package.res.resver | where {$_.id -eq "1057002654"}
$node.id = '123'
$myxml.Save($path)

这只是一个快速测试,以查看是否可以达到所需的水平并进行更新,但是失败了:
Property 'id' cannot be found on this object; make sure it exists and is settable.

然后,我尝试了类似的方法:
$xml = [xml](Get-Content 'C:\TEST\package.xml')
$nodes = $xml.SelectNodes("//*[@res]")
foreach ($node in $nodes) {

$node.Attributes | %{$_} > C:\TEST\disk.txt }

这实际上将属性中的所有值都写入.txt文件,但似乎找不到实际执行我想要的方法。

/编辑-纠正了我的实际意图。最终得到这样的代码,其中 $ Ref 是数组 $ References 中的一项
foreach ($Ref in $References)
{
$xml = [xml](Get-Content 'C:\TEST\package.xml')
$res = $xml.SelectSingleNode('//res[@id = $Ref]/child::resver')
$resource = $res.id + ".txt"

# more stuff here
}

虽然出现此错误:
Exception calling "SelectSingleNode" with "1" argument(s): "Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."
At C:\TEST\script.ps1:22 char:38
+ [string] $res = $xml.SelectSingleNode <<<< ('//res[@id = $Ref]/child::resver')
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

最佳答案

在找到具有正确ID的parent::节点后,使用<resver />轴查找适当的父节点:

$path = 'C:\TEST\package.xml'
$myxml = [xml](Get-Content $path)
# Locate resver, select parent res node
$res = $myxml.SelectSingleNode('//resver[@id = "1057014163"]/parent::res')
# Update id
$res.SetAttribute('id','123')
# Save document
$myxml.Save($path)

您还可以为父节点使用 ..别名:
//resver[@id = "1057014163"]/..

as PetSerAl notes,选择具有所描述的子节点的 res节点:
//res[resver/@id = "1057014163"]

通过另一种方法(根据父节点 resver查找所有 id子节点 id值):
$ids = $myxml.SelectNodes('//res[@id = "1057002654"]/resver[@id]') |Select -Expand id

(注意使用 SelectNodes()而不是 SelectSingleNode())

关于xml - 在Powershell中解析XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38471280/

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