【问题标题】:Trouble with converting a xml into a csv with Powershell使用 Powershell 将 xml 转换为 csv 的问题
【发布时间】:2021-02-17 06:53:36
【问题描述】:

所以我有这个非常规的 xml,我想将其转换为 csv,我已经到了不知道在哪里进一步查找的地步。

<?xml version="1.0" encoding="UTF-16"?>
-<Records count="88">

-<Record parentId="0" moduleId="618" levelGuid="bf1e60be-cae0-428d-88eb-60cce2ca7e80" levelId="416" contentId="889443">
<Field type="1" guid="55ef2cec-bcff-4bff-ad83-43f55205fad2" id="24035">Exception Requests >> Exception Requests</Field>
<Field type="1" guid="de997cd6-e53c-4ff1-81e9-17a9f98b3043" id="24036">Name</Field>
<Field type="1" guid="d4d1d2ad-c4af-454a-afad-851617a94874" id="24037">Additional Access</Field>
<Field type="1" guid="0f464953-42d1-4487-96b1-6b3d1ba85558" id="24038">Accès supplémentaire</Field>
<Field type="1" guid="91cf5fd2-70dc-4e37-bca9-046851e2c763" id="24039">cada8def-650f-443d-9ef3-53b74ecc16b2</Field>
<Field type="1" guid="5a5bdeb2-8f98-4f56-8c77-e1294b4e1cf1" id="24040">476</Field>
<Field type="1" guid="2a67dc86-c01d-4927-8a08-7557543248d2" id="24041">1</Field>
<Field type="1" guid="395a0e50-52cd-4d61-8653-94ad41f5d7da" id="24042">b809048d0be00ec524a7c7c95c1a1f8a</Field>
</Record>

-<Record parentId="0" moduleId="618" levelGuid="bf1e60be-cae0-428d-88eb-60cce2ca7e80" levelId="416" contentId="889317">
<Field type="1" guid="55ef2cec-bcff-4bff-ad83-43f55205fad2" id="24035">Exception Request Extensions >> Exception Request Extensions</Field>
<Field type="1" guid="de997cd6-e53c-4ff1-81e9-17a9f98b3043" id="24036">Name</Field>
<Field type="1" guid="d4d1d2ad-c4af-454a-afad-851617a94874" id="24037">Most Recent Approved Expiration Date Helper</Field>
<Field type="1" guid="0f464953-42d1-4487-96b1-6b3d1ba85558" id="24038">Aide sur la date max.</Field>
<Field type="1" guid="91cf5fd2-70dc-4e37-bca9-046851e2c763" id="24039">8b3fbad5-2806-4a64-8e2b-fe5dfaa4562d</Field>
<Field type="1" guid="5a5bdeb2-8f98-4f56-8c77-e1294b4e1cf1" id="24040">8098</Field>
<Field type="1" guid="2a67dc86-c01d-4927-8a08-7557543248d2" id="24041">1</Field>
<Field type="1" guid="395a0e50-52cd-4d61-8653-94ad41f5d7da" id="24042">594a6240a3a606cf9720189a5f15980c</Field>
</Record>
</Records>

我想要的是让每条记录的字段成为列,id 可以用作名称,后面的文本是值。 (最后一张)

现在这就是我所拥有的:

#read from file
"reading..."
[xml]$inputFile = Get-Content "Exportation_Field.xml"
"exporting..."
#export xml as csv
if ($inputFile.Records.childnodes )
    {
    $inputFile.Records.record.childnodes | Select-Object "#text" | Export-Csv "exportCSV.csv" -NoTypeInformation -Delimiter:";" -Encoding:UTF8 
    "complete"
    }

这会在单个列中一个接一个地返回所有字段的所有#text

这就是我想要得到的:outputCSV

我觉得某种循环可能会有所帮助,但作为 PowerShell 新手,我不知道如何在这种情况下使用它们。

这是预期的结果:

expectedCSV

提前致谢。

【问题讨论】:

  • 示例 XML 无效,缺少关闭记录?
  • 因为有 88 条记录,放整个东西可能太长了

标签: xml powershell csv converters


【解决方案1】:

为了获得您想要的结果,您需要做的是将具有每个 ID 的对象作为属性。我们可以使用很多工具,在这个例子中我将使用Select-XML

select-xml -Path "Exportation_Field.xml" -XPath "//Record" |
    ForEach-Object {
        $_.node.field | ForEach-Object -Begin {$hash = [ordered]@{}} -Process {
            $hash.add($_.id,$_.'#text')
        } -End {[PSCustomObject]$hash}
}

在这个例子中,你最终会得到 2 个对象

24035 : Exception Requests >> Exception Requests
24036 : Name
24037 : Additional Access
24038 : Accès supplémentaire
24039 : cada8def-650f-443d-9ef3-53b74ecc16b2
24040 : 476
24041 : 1
24042 : b809048d0be00ec524a7c7c95c1a1f8a

24035 : Exception Request Extensions >> Exception Request Extensions
24036 : Name
24037 : Most Recent Approved Expiration Date Helper
24038 : Aide sur la date max.
24039 : 8b3fbad5-2806-4a64-8e2b-fe5dfaa4562d
24040 : 8098
24041 : 1
24042 : 594a6240a3a606cf9720189a5f15980c

只需在末尾添加Export-Csv。我选择使用Foreach-Object cmdlet的-Begin-End参数,也可以这样写。

select-xml -Path "Exportation_Field.xml" -XPath "//Record" |
    ForEach-Object {
        $hash = [ordered]@{}
        $_.node.field | ForEach-Object {
            $hash.add($_.id,$_.'#text')
        }
        [PSCustomObject]$hash
} | Export-Csv -Path OutFile.csv -NoTypeInformation

【讨论】:

  • 谢谢你,它搞砸了,我只需要添加 -Delimiter:";" -Encoding:UTF8 after -noTypeInformation.
  • 太棒了!请考虑通过单击复选标记接受它作为解决方案。这样其他用户就知道这是一个有效的答案。
猜你喜欢
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 2020-06-10
  • 1970-01-01
  • 1970-01-01
  • 2019-12-11
相关资源
最近更新 更多