如何使用PowerShell读取XML文件并筛选所需数据
如何使用PowerShell读取XML文件并从包含大量标记的文件中提取数据?我正在使用下面的代码提取标签,但无法从子标签读取数据如何使用PowerShell读取XML文件并筛选所需数据,xml,powershell,Xml,Powershell,如何使用PowerShell读取XML文件并从包含大量标记的文件中提取数据?我正在使用下面的代码提取标签,但无法从子标签读取数据 $xmlFile= "D:\Testing\TestcasesOutput\1ac.xml" $xmlConfig = [System.Xml.XmlDocument](Get-Content $xmlFile) $XmlDocument.Breakfast_menu.price 我希望输出读取整个xml文件,但无法读取整个xml文件 <?xml versio
$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>
华夫饼
$5.95
我们的两个著名的比利时华夫饼干,里面有很多真正的枫树
糖浆
650
草莓比利时华夫饼干
$7.95
轻的比利时华夫饼干,上面覆盖草莓和鲜奶
奶油
900
浆果比利时华夫饼干
$8.95
淡比利时华夫饼干,上面覆盖着各种各样的新鲜水果
浆果和鲜奶油
900
法式吐司
$4.50
用我们自制的酸面团做成的厚片
面包
600
家庭式早餐
$6.95
两个鸡蛋,培根或香肠,烤面包,还有我们最受欢迎的杂烩
布朗
950
使用PowerShell读取XML非常简单
假设您的xml文件类似于以下内容:
接下来,您可以使用此$xml
对象的属性来获取要从中提取的内容:
例如,循环浏览所有
项并输出所需信息
$xml.breakfast_menu.food | ForEach-Object {
[PSCustomObject]@{
'MenuItem' = $_.food
'Price' = $_.price
}
}
此输出中的结果:
或者只选择“比利时华夫饼”项目:
$xml.breakfast_menu.food | Where-Object { $_.food -eq 'Belgian Waffles' } |
Select-Object @{Name = 'MenuItem'; Expression = {$_.food}}, Price
产出:
如果你所追求的只是某一食品的价格,你可以这样做:
$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
使用PowerShell读取XML非常简单 假设您的xml文件类似于以下内容: 接下来,您可以使用此
$xml
对象的属性来获取要从中提取的内容:
例如,循环浏览所有
项并输出所需信息
$xml.breakfast_menu.food | ForEach-Object {
[PSCustomObject]@{
'MenuItem' = $_.food
'Price' = $_.price
}
}
此输出中的结果:
或者只选择“比利时华夫饼”项目:
$xml.breakfast_menu.food | Where-Object { $_.food -eq 'Belgian Waffles' } |
Select-Object @{Name = 'MenuItem'; Expression = {$_.food}}, Price
产出:
如果你所追求的只是某一食品的价格,你可以这样做:
$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
我正在使用下面的代码提取早餐菜单标签,但无法从早餐菜单子标签读取数据$xmlFile=“D:\Testing\TestcasesOutput\1ac.xml”$xmlConfig=[System.xml.XmlDocument](获取内容$xmlFile)$XmlDocument.breakth\u menu.price不要评论您自己的问题,而是在问题后面附加这些重要的行。请将您的问题和xml(相关部分)放在那里。接下来,请看@quantummind的内容comments@Theo比利时华夫饼5.95美元我们著名的比利时华夫饼中有两种加了大量真正枫糖浆。。如注释所示,单击问题下方的
edit
,并将信息放在一个列表中。永远不要在评论中粘贴重要信息或代码,因为这样会很快变得不可读。我正在使用下面的代码提取早餐菜单标签,但无法从早餐菜单子标签中读取数据$xmlFile=“D:\Testing\TestcasesOutput\1ac.xml”$xmlConfig=[System.xml.XmlDocument](获取内容$xmlFile)$XmlDocument.breakth\u menu.price不要评论您自己的问题,而是在问题后面附加这些重要的行。请将您的问题和xml(相关部分)放在那里。接下来,请看@quantummind的内容comments@Theo比利时华夫饼5.95美元我们著名的比利时华夫饼中有两种加了大量真正枫糖浆。。如注释所示,单击问题下方的edit
,并将信息放在一个列表中。永远不要在注释中粘贴重要信息或代码,因为这样会很快变得不可读。是的,这很有帮助,但我有另一个xml文件,它有3000行,通过使用与您描述的相同的方法,我解析了数据,但无法获得它。您能确认一下大型xml文件有什么问题吗?@Ali 3000行应该没有问题。。仔细研究结构。这是否与此xml相同,只是更大?谢谢您的支持。请您进一步解释如何解析路径上的100xml文件,并对所需数据进行grep处理,删除剩余数据。如果您能进一步指导我,这将非常有用??通过使用下面的脚本,我能够处理单个xml文件[xml]$xmlDocument=Get Content-Path“D:\Testing\TestcasesOutput\1ac\Export25.xml”$xmlConfig=[System.xml.xmlDocument](Get Content$xmlFile)$xmlDocument.Content.sessionEventTypeItem但当我试图处理100个xml文件时,它不会显示输出。[xml]$xmlDocument=Get Content-Path“D:\Testing\TestcasesOutput\1ac\Export25.xml”$xmlConfig=[System.xml.xmlDocument](Get Content$xmlFile)$xmlDocument.Content.sessioneventtypeitem是的,这很有帮助,但我有另一个xml文件,它有3000行,通过使用与您描述的相同的方法,我解析了数据,但无法获取它。您能确认一下大型xml文件有什么问题吗?@Ali 3000行应该没有问题。。仔细研究结构。这是否与此xml相同,只是更大?谢谢您的支持。请您进一步解释如何解析路径上的100xml文件,并对所需数据进行grep处理并删除