首页 > 解决方案 > 如何使用 PowerShell 读取 XML 文件并过滤所需数据

问题描述

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

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

我希望输出能够读取整个 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>Strawberry Belgian Waffles</food>
        <price>$7.95</price>
        <description>Light Belgian waffles covered with strawberries and whipped 
        cream</description>
        <calories>900</calories>
    </food>
    <food>
        <food>Berry-Berry Belgian Waffles</food>
        <price>$8.95</price>
        <description>Light Belgian waffles covered with an assortment of fresh 
        berries and whipped cream</description>
        <calories>900</calories>
    </food>
    <food>
        <food>French Toast</food>
        <price>$4.50</price>
        <description>Thick slices made from our homemade sourdough 
        bread</description>
        <calories>600</calories>
    </food>
    <food>
    <food>Homestyle Breakfast</food>
        <price>$6.95</price>
        <description>Two eggs, bacon or sausage, toast, and our ever-popular hash 
        browns</description>
        <calories>950</calories>
    </food>
</breakfast_menu>

标签: xmlpowershell

解决方案


使用 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

推荐阅读