如何使用PowerShell读取XML文件并过滤所需的数据

时间:2019-05-05 13:27:17

标签: xml powershell

如何使用PowerShell读取XML文件并从具有大量标签的文件中提取数据?我正在使用下面的代码提取标签,但无法从子标签中读取数据。

   cat1  cat2   name
0   1.0   0.0  name1
1   0.0   1.0  name2
2   1.0   1.0  name3

我希望输出读取整个xml文件,但无法读取整个xml文件。

$xmlFile= "D:\Testing\TestcasesOutput\1ac.xml"
$xmlConfig = [System.Xml.XmlDocument](Get-Content $xmlFile)
$XmlDocument.Breakfast_menu.price

1 个答案:

答案 0 :(得分:2)

使用PowerShell读取XML非常简单。

假设您的xml文件看起来与此类似:

<?xml version="1.0" encoding="UTF-8"?> 
<breakfast_menu> 
    <food> 
        <food>Belgian Waffles</food>
        <price>$5.95</price>
        <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
        <calories>650</calories>
    </food>
    <food> 
        <food>Fried Egg</food>
        <price>$1.80</price>
        <description>blahblah</description>
        <calories>3500</calories>
    </food>
</breakfast_menu>

您只需阅读并让PowerShell使用此文件将其解析为一个对象

[xml]$xml = Get-Content 'D:\Testing\TestcasesOutput\1ac.xml'

接下来,您可以使用此$xml对象的属性来获取要从中提取的任何内容:

例如,遍历所有<food>项并输出所需的信息

$xml.breakfast_menu.food | ForEach-Object {
    [PSCustomObject]@{
        'MenuItem' = $_.food
        'Price'    = $_.price
    }
}

结果如下:

MenuItem        Price
--------        -----
Belgian Waffles $5.95
Fried Egg       $1.80

或仅选择“比利时华夫饼”中的一项:

$xml.breakfast_menu.food | Where-Object { $_.food -eq 'Belgian Waffles' } | 
                           Select-Object @{Name = 'MenuItem'; Expression = {$_.food}}, Price

输出:

MenuItem        price
--------        -----
Belgian Waffles $5.95

如果您所追求的只是某种食品的价格,则可以执行以下操作:

$xml.breakfast_menu.food | Where-Object { $_.food -eq 'Belgian Waffles' } | 
                           Select-Object -ExpandProperty Price

甚至缩短该代码:

($xml.breakfast_menu.food | Where-Object { $_.food -eq 'Belgian Waffles' }).price

希望能解释


修改

如果您需要对多个xml文件执行此操作,并且这些文件位于相同根路径内,则可以使用Get-ChildItem循环获取xml文件并按如下方式进行处理我举的例子。

Get-ChildItem -Path 'ROOTFOLDER OF THE FOLDERS WHERE THE XML FILES ARE KEPT' -Filter '*.xml' -File -Recurse | 
    ForEach-Object {
        [xml]$xml = Get-Content -Path $_.FullName
        # in this example simply output the menu items and their price for each xml file
        foreach ($item in $xml.breakfast_menu.food) {
            [PSCustomObject]@{
                'File'     = $_.FullName    # added the file FullName so you know where the item came from
                'MenuItem' = $item.food
                'Price'    = $item.price
            }
        }
    }

或从多个位置:

$folders = 'D:\Testing\TestcasesOutput\1ac7b5a0-2d62-403c-8394-5bd33330cbe7',
           'D:\Testing\TestcasesOutput\227c619a-b7d1-4da6-8fe5-f2c923ddcb7a',
           'D:\Testing\TestcasesOutput\d4370ae1-643f-4c44-ba41-7f640afcc276'

$result = Get-ChildItem -Path $folders -Filter '*.xml' -File | 
    ForEach-Object {
        [xml]$xml = Get-Content -Path $_.FullName
        # in this example simply output the menu items and their price for each xml file
        foreach ($item in $xml.breakfast_menu.food) {
            [PSCustomObject]@{
                'File'     = $_.FullName
                'MenuItem' = $item.food
                'Price'    = $item.price
            }
        }
    }

#output to screen:
$result

# output to CSV
$result | Export-Csv -Path 'PATH AND FILENAME FOR THE OUTPUT CSV FILE' -NoTypeInformation