[英]select multiple nodes from multiple xml files in powershell?
I'am new to Poweshell and have about 100 xml files with the same structure and I want to list two nodes from all of them in one list.我是 Poweshell 的新手,大约有 100 个结构相同的 xml 文件,我想在一个列表中列出所有节点中的两个节点。
I have succeeded from one xml file but then I specify a specific path to the xml file, so somehow I need to do a loop through all the xml files in the folder ("C:\Users\mittm\Documents\ps). I have succeeded from one xml file but then I specify a specific path to the xml file, so somehow I need to do a loop through all the xml files in the folder ("C:\Users\mittm\Documents\ps).
$Path = "C:\Users\mittm\Documents\ps\myxml.xml"
$xml = New-Object -TypeName XML
$xml.Load($Path)
$Xml.catalog.book | Select-Object -Property title, author
Gives the result给出结果
title author
----- ------
Title 1 Author 1
But I want the result但我想要结果
title author
----- ------
Title 1 Author 1
Title 2 Author 2
Title 3 Author 3
xml-file 1 xml 文件 1
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Author 1</author>
<title>Title 1</title>
<price>44.95</price>
</book>
</catalog>
xml-file 2 xml 文件 2
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Author 2</author>
<title>Title 2</title>
<price>44.95</price>
</book>
</catalog>
And so on with about 100 xml-files.依此类推,大约有 100 个 xml 文件。 My real xml has more tags than my examples inside the book but I still want to select two but shortened it to save space.
我真正的 xml 的标签比我书中的例子多,但我仍然想要 select 两个,但缩短了它以节省空间。
All you are missing is Get-ChildItem
to query all the files (use -Recurse
switch if you need to go into subfolders)您所缺少的是
Get-ChildItem
来查询所有文件(如果您需要将 go 放入子文件夹,请使用-Recurse
开关)
From there, by assigning the foreach to a variable, you should end up with the list containings all the results from all the xmls.从那里,通过将 foreach 分配给一个变量,您最终应该得到一个包含所有 xml 的所有结果的列表。
$AllXmls = Get-ChildItem -Path 'Path\to the xmls files' -Filter '*.xml' -File #-Recurse
$Output = foreach ($Path in $AllXmls.FullName) {
$xml = New-Object -TypeName XML
$xml.Load($Path)
$Xml.catalog.book | Select-Object -Property title, author
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.