[英]Powershell convert xml to csv
我必须将我的 xml 文件转换为 csv 文件。 我真的不知道该怎么做。 看了很多帖子,就是没看懂。。。
这是我的 xml:
<?xml version="1.0" encoding="UTF-8"?>
<license_usage xmlns="http://www.example.com/report">
<start_time timestamp="1643670000">2022-01-31T23:00:00+00:00</start_time>
<end_time timestamp="1645615967">2022-02-23T11:32:47+00:00</end_time>
<group_by>hour</group_by>
<license_time_intervals>
<license_time_interval timestamp="1643670000" datetime="2022-01-31T23:00:00+00:00">
<license_count license_type="support_full" reason="login">0</license_count>
<license_count license_type="support_full" reason="extended_contact">0</license_count>
<license_count license_type="support_full">0</license_count>
</license_time_interval>
<license_time_interval timestamp="1643673600" datetime="2022-02-01T00:00:00+00:00">
<license_count license_type="support_full" reason="login">2</license_count>
<license_count license_type="support_full" reason="extended_contact">0</license_count>
<license_count license_type="support_full">2</license_count>
</license_time_interval>
<license_time_interval timestamp="1643677200" datetime="2022-02-01T01:00:00+00:00">
<license_count license_type="support_full" reason="login">0</license_count>
<license_count license_type="support_full" reason="extended_contact">0</license_count>
<license_count license_type="support_full">0</license_count>
</license_time_interval>
<license_time_interval timestamp="1643680800" datetime="2022-02-01T02:00:00+00:00">
<license_count license_type="support_full" reason="login">0</license_count>
<license_count license_type="support_full" reason="extended_contact">0</license_count>
<license_count license_type="support_full">0</license_count>
</license_time_interval>
<license_time_interval timestamp="1643684400" datetime="2022-02-01T03:00:00+00:00">
<license_count license_type="support_full" reason="login">0</license_count>
<license_count license_type="support_full" reason="extended_contact">0</license_count>
<license_count license_type="support_full">0</license_count>
</license_time_interval>
</license_time_intervals>
</license_usage>
我想将它与 Powershell 转换为 csv,如下所示:
license_count/0/_license_type,license_count/0/_reason,license_count/0/__text,license_count/1/_license_type,license_count/1/_reason,license_count/1/__text,license_count/2/_license_type,license_count/2/__text,_timestamp,_datetime
support_full,login,0,support_full,extended_contact,0,support_full,0,1643670000,2022-01-31T23:00:00+00:00
support_full,login,2,support_full,extended_contact,0,support_full,2,1643673600,2022-02-01T00:00:00+00:00
support_full,login,0,support_full,extended_contact,0,support_full,0,1643677200,2022-02-01T01:00:00+00:00
support_full,login,0,support_full,extended_contact,0,support_full,0,1643680800,2022-02-01T02:00:00+00:00
support_full,login,0,support_full,extended_contact,0,support_full,0,1643684400,2022-02-01T03:00:00+00:00
support_full,login,0,support_full,extended_contact,0,support_full,0,1643688000,2022-02-01T04:00:00+00:00
support_full,login,3,support_full,extended_contact,0,support_full,3,1643691600,2022-02-01T05:00:00+00:00
and so on
所以我调用 xmlFile:
$xmlFile = Get-Content "C:\temp\example.xml"
go怎么上?
你能帮帮我吗?
太感谢了!!!
这是对数据的不同解释:
# return XmlDocument type from xml content
$xml = [xml](Get-Content "C:\temp\example.xml")
# drill down to the license time intervals
$xml.license_usage.license_time_intervals.license_time_interval | Foreach-Object {
# current license time interval object
$interval = $_
# iterate license_count nodes
# use calculated properties to mix higher level properties with nested properties
# license count comes from the node text
$_.license_count | Select @{n='timestamp';e={$interval.timestamp}},
@{n='datetime';e={$interval.datetime}},
license_type,reason,
@{n='license_count';e={$_.'#text'}}
} | Export-Csv output.csv -NoType
output CSV 将包含标头timestamp,datetime,license_type,reason,license_count
。 请注意,这将导致多行包含相同的timestamp
和datetime
时间值,因为它们具有多个关联的license_count
节点。 就我个人而言,在 PowerShell 和 CSV 中,这对我来说更容易阅读和排序。使用这些属性进行扩展可能需要代码更加动态,因为您可能并不总是知道会有多少个license_count
节点。 只要模式保持不变,无论节点数量如何,此代码都应该有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.