Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Powershell将嵌套xml转换为csv时遇到问题_Xml_Powershell_Csv - Fatal编程技术网

使用Powershell将嵌套xml转换为csv时遇到问题

使用Powershell将嵌套xml转换为csv时遇到问题,xml,powershell,csv,Xml,Powershell,Csv,我有一个嵌套的XML,需要使用Powershell将其转换为CSV。 不幸的是,我还处于初级阶段,无法用现有的线程解决这个问题,我在网上发现 我尝试将XML文件读入Powershell并创建一个新对象,但我的csv导出甚至不包含那个不充分的结果…:( 我拥有的XML文件如下所示: Location;Date/Time;Sold Cars car;Sold Cars Auto Germany; 2019-04-22T00:00:00+02:00; 7.3;4.0 Germany; 2019-04-

我有一个嵌套的XML,需要使用Powershell将其转换为CSV。 不幸的是,我还处于初级阶段,无法用现有的线程解决这个问题,我在网上发现

我尝试将XML文件读入Powershell并创建一个新对象,但我的csv导出甚至不包含那个不充分的结果…:(

我拥有的XML文件如下所示:

Location;Date/Time;Sold Cars car;Sold Cars Auto Germany; 2019-04-22T00:00:00+02:00; 7.3;4.0 Germany; 2019-04-22T00:00:00+02:00; 7.8;5.0 Germany; 2019-04-22T00:00:00+02:00; 7.0;3.0 Germany; 2019-04-22T00:00:00+02:00; 6.0;4.0 USA; 2019-04-22T00:00:00+02:00; 5.1;3.0 USA; 2019-04-22T00:00:00+02:00; 4.1;6.0 USA; 2019-04-22T00:00:00+02:00; 3.6;1.0 USA; 2019-04-22T00:00:00+02:00; 3.1;8.0
$combined = $result | Group-Object -Property DateTime, Location | ForEach-Object {
    foreach ($location in ($_.Group | Group-Object Location)) {
        # create an output object and put in the Location property here
        $objOut = New-Object -TypeName PSObject
        $objOut | Add-Member -MemberType NoteProperty -Name 'Location' -Value ($location.Name)
        foreach ($date in ($location.Group | Group-Object DateTime)) {
            # add the DateTime property
            $objOut | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value ($date.Name)
            foreach ($unit in $_.Group) {
                # join the other two properties to the $objOut object:
                # I do not want to hard-code the property names here, 
                # so use Select-Object to get the remaining props.
                $sold = $unit | Select-Object * -ExcludeProperty Location, DateTime
                foreach ($thing in $sold.psobject.properties | Where-Object { ($_.Value) }) {
                    # if you want the numbers as floating-point numbers, do this:
                    # $objOut | Add-Member -MemberType NoteProperty -Name $($thing.Name) -Value ([double]$thing.Value)
                    # like below, these values will be output as string
                    $objOut | Add-Member -MemberType NoteProperty -Name $($thing.Name) -Value ($thing.Value)
                }
            }
        }
        $objOut
    }
}

# output on screen
$combined | Format-Table -AutoSize
# output to CSV file
$combined | Export-Csv -Path 'D:\test_Grouped.csv' -Encoding UTF8 -NoTypeInformation

我的目标是这样的:

Location;Date/Time;Sold Cars car;Sold Cars Auto Germany; 2019-04-22T00:00:00+02:00; 7.3;4.0 Germany; 2019-04-22T00:00:00+02:00; 7.8;5.0 Germany; 2019-04-22T00:00:00+02:00; 7.0;3.0 Germany; 2019-04-22T00:00:00+02:00; 6.0;4.0 USA; 2019-04-22T00:00:00+02:00; 5.1;3.0 USA; 2019-04-22T00:00:00+02:00; 4.1;6.0 USA; 2019-04-22T00:00:00+02:00; 3.6;1.0 USA; 2019-04-22T00:00:00+02:00; 3.1;8.0
$combined = $result | Group-Object -Property DateTime, Location | ForEach-Object {
    foreach ($location in ($_.Group | Group-Object Location)) {
        # create an output object and put in the Location property here
        $objOut = New-Object -TypeName PSObject
        $objOut | Add-Member -MemberType NoteProperty -Name 'Location' -Value ($location.Name)
        foreach ($date in ($location.Group | Group-Object DateTime)) {
            # add the DateTime property
            $objOut | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value ($date.Name)
            foreach ($unit in $_.Group) {
                # join the other two properties to the $objOut object:
                # I do not want to hard-code the property names here, 
                # so use Select-Object to get the remaining props.
                $sold = $unit | Select-Object * -ExcludeProperty Location, DateTime
                foreach ($thing in $sold.psobject.properties | Where-Object { ($_.Value) }) {
                    # if you want the numbers as floating-point numbers, do this:
                    # $objOut | Add-Member -MemberType NoteProperty -Name $($thing.Name) -Value ([double]$thing.Value)
                    # like below, these values will be output as string
                    $objOut | Add-Member -MemberType NoteProperty -Name $($thing.Name) -Value ($thing.Value)
                }
            }
        }
        $objOut
    }
}

# output on screen
$combined | Format-Table -AutoSize
# output to CSV file
$combined | Export-Csv -Path 'D:\test_Grouped.csv' -Encoding UTF8 -NoTypeInformation

也许不完全是您想要的输出所显示的,但这可能会有所帮助

注意:我使用了一个here字符串来表示xml

[xml]$xml = Get-Content "C:\Users\[me]\Convert_XML_to_CSV\cars.xml"
守则:

[xml]$xml = @'
<?xml version="1.0" encoding="ISO-8859-1"?>
<Data source="Jhonny" datetime="2019-04-23T10:07:50+02:00" timezone="Europe">
    <dealerships>
        <location name="Germany">
            <series parameter="Sold Cars" unit="car">
                <value datetime="2019-04-22T00:00:00+02:00" value="7.3"/>
                <value datetime="2019-04-22T01:00:00+02:00" value="7.8"/>
                <value datetime="2019-04-22T02:00:00+02:00" value="7.0"/>
                <value datetime="2019-04-22T03:00:00+02:00" value="6.0"/>
            </series>
            <series parameter="Sold Cars" unit="Auto">
                <value datetime="2019-04-22T00:00:00+02:00" value="4.0"/>
                <value datetime="2019-04-22T01:00:00+02:00" value="4.0"/>
                <value datetime="2019-04-22T02:00:00+02:00" value="4.0"/>
                <value datetime="2019-04-22T03:00:00+02:00" value="4.0"/>
            </series>
        </location>
        <location name="USA">
            <series parameter="Sold Cars" unit="car">
                <value datetime="2019-04-22T00:00:00+02:00" value="5.1"/>
                <value datetime="2019-04-22T01:00:00+02:00" value="4.1"/>
                <value datetime="2019-04-22T02:00:00+02:00" value="3.6"/>
                <value datetime="2019-04-22T03:00:00+02:00" value="3.1"/>
            </series>
            <series parameter="Sold Cars" unit="Auto">
                <value datetime="2019-04-22T00:00:00+02:00" value="3.0"/>
                <value datetime="2019-04-22T01:00:00+02:00" value="3.0"/>
                <value datetime="2019-04-22T02:00:00+02:00" value="3.0"/>
                <value datetime="2019-04-22T03:00:00+02:00" value="3.0"/>
            </series>
        </location>
    </dealerships>
</Data>
'@ 

$result = foreach ($item in $xml.Data.dealerships.location) {
    $location = $item.Name

    # get the different column names
    $units = $item.series | ForEach-Object { '{0} {1}' -f $_.parameter, $_.unit}

    # loop through the series
    foreach ($series in $item.series) {
        # and the values
        foreach ($value in $series.value) {
            # since you are using PowerShell 2.0, create the output object like this
            $objOut = New-Object -TypeName PSObject
            $objOut | Add-Member -MemberType NoteProperty -Name 'Location' -Value $location
            $objOut | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value $value.datetime

            $thisUnit = '{0} {1}' -f $series.parameter, $series.unit
            # add the different units as property.
            foreach ($unit in $units) { 
                $val = if ($unit -eq $thisUnit) { $value.value } else { '' }
                $objOut | Add-Member -MemberType NoteProperty -Name $unit -Value $val 
            }

            # output the object
            $objOut
        }
    }
}

# output on screen
$result | Format-Table -AutoSize
# output to CSV file
$result | Export-Csv -Path 'D:\test.csv' -Encoding UTF8 -NoTypeInformation

更新

根据评论中的要求,您可以进一步组合/分组上述代码中的
$result
数组,如下所示:

Location;Date/Time;Sold Cars car;Sold Cars Auto Germany; 2019-04-22T00:00:00+02:00; 7.3;4.0 Germany; 2019-04-22T00:00:00+02:00; 7.8;5.0 Germany; 2019-04-22T00:00:00+02:00; 7.0;3.0 Germany; 2019-04-22T00:00:00+02:00; 6.0;4.0 USA; 2019-04-22T00:00:00+02:00; 5.1;3.0 USA; 2019-04-22T00:00:00+02:00; 4.1;6.0 USA; 2019-04-22T00:00:00+02:00; 3.6;1.0 USA; 2019-04-22T00:00:00+02:00; 3.1;8.0
$combined = $result | Group-Object -Property DateTime, Location | ForEach-Object {
    foreach ($location in ($_.Group | Group-Object Location)) {
        # create an output object and put in the Location property here
        $objOut = New-Object -TypeName PSObject
        $objOut | Add-Member -MemberType NoteProperty -Name 'Location' -Value ($location.Name)
        foreach ($date in ($location.Group | Group-Object DateTime)) {
            # add the DateTime property
            $objOut | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value ($date.Name)
            foreach ($unit in $_.Group) {
                # join the other two properties to the $objOut object:
                # I do not want to hard-code the property names here, 
                # so use Select-Object to get the remaining props.
                $sold = $unit | Select-Object * -ExcludeProperty Location, DateTime
                foreach ($thing in $sold.psobject.properties | Where-Object { ($_.Value) }) {
                    # if you want the numbers as floating-point numbers, do this:
                    # $objOut | Add-Member -MemberType NoteProperty -Name $($thing.Name) -Value ([double]$thing.Value)
                    # like below, these values will be output as string
                    $objOut | Add-Member -MemberType NoteProperty -Name $($thing.Name) -Value ($thing.Value)
                }
            }
        }
        $objOut
    }
}

# output on screen
$combined | Format-Table -AutoSize
# output to CSV file
$combined | Export-Csv -Path 'D:\test_Grouped.csv' -Encoding UTF8 -NoTypeInformation
这将导致:


这一个有点棘手。我通过使用PowerShell的本地解析功能解析XML来处理它,然后通过
.location
逐步遍历节点,给我们一个按位置划分的列表(因此我们有一个用于美国,一个用于德国,等等)

在第一个循环中,每个位置有两个系列,一个是一辆车,另一个是一辆汽车。接下来,我们找到一个是一辆车的
系列
,以获得所有售出的汽车。然后我们通过这些
找到每辆车的
系列

在嵌套最深的循环中,
cars
,我们从
Auto
系列中找到一条匹配记录,按日期时间匹配

这为我们提供了以PowerShell 2.0格式创建PSCustomObject所需的所有属性。我已进行了测试,所需的输出看起来与您所需的完全一致

$dealerships = ([xml]$x).Data.dealerships.location

foreach ($location in $dealerships){
    $cars = $location.series | Where-Object {$_.unit -eq 'car'}
    foreach ($car in $cars.value){
        $auto = $location.series | Where-Object {$_.unit -eq 'Auto'} | Select-Object -ExpandProperty value | Where-Object {$_.datetime -eq $car.datetime}

        $ObjectProperties = @{
            Location = $location.name
            DateTime = $car.datetime
            SoldCars = $car.value
            SoldAutos= $auto.value
        }
        New-Object PSObject -Property $ObjectProperties
    }
}

您的目标是哪个版本的PowerShell?它是否需要在PowerShell 2.0(Windows 7默认版本)中工作?嗨,Mathias,很遗憾,我正在使用W7。将在几个月后升级,但到目前为止,只升级PowerShell 2.0。br Bananajoeth这是一种简洁的方法:
$units=$item.series | ForEach对象{{{0}{1}'-f$\.parameter,$\.unit}
你好,提奥,非常感谢!我可以重新创建您的结果。我相信这应该可以完成工作。祝您玩得愉快!提奥,有没有办法,我可以把汽车和汽车放在同一条线上,这样我就可以节省位置和日期时间的两倍?@BananaJoe我已经更新了我的答案,将结果合并到一个更精简的输出中。希望您喜欢。像这样工作魅力。谢谢你@Theo,太神奇了!我正在试图理解答案:)嘿,刚刚看到你的答案。我明天也会试试这个。无论如何谢谢你的帮助。从第一眼看,这似乎给了我什么,我一直在寻找,所以热衷于尝试它明天。很好的一天!您好,FoxDeploy,我现在已经测试了您的解决方案,发现了一个您在测试数据中无法遇到的问题。通过日期时间匹配汽车和汽车。这意味着,如果存在一个不存在car值的Auto值,则该行的输出不会出现任何结果。我能绕过这个吗?FoxDeploy,我会追求Theos的解决方案。但是非常感谢你的方法!我可以从中学到的东西:)