当前位置 : 主页 > 网页制作 > xml >

将XML转换为CSV不起作用

来源:互联网 收集:自由互联 发布时间:2021-06-13
XML文件内容: ?xml version="1.0"?servstatetitle2018-06-11 12:00:01/titleservicesservicenameOracle Weblogic AdminServer 1/namestateRUNNING./state/serviceservicenameOracle WebLogic NodeManager 1/namestateRUNNING./state/serviceservicename
XML文件内容:

<?xml version="1.0"?>
<servstate>
<title>2018-06-11  12:00:01</title>
<services>
<service>
<name>Oracle Weblogic AdminServer 1</name>
<state>RUNNING.</state>
</service>
<service>
<name>Oracle WebLogic NodeManager 1</name>
<state>RUNNING.</state>
</service>
<service>
<name>OracleProcessManager_1</name>
<state>RUNNING.</state>
</service>
<service>
<name>OracleProcessManager_2</name>
<state>RUNNING.</state>
</service>
<service>
<name>Oracle WebLogic NodeManager 2</name>
<state>RUNNING.</state>
</service>
<service>
<name>Oracle Weblogic AdminServer 2</name>
<state>RUNNING.</state>
</service>
</services>
</servstate>

我是PowerShell的新手并编写了一个脚本来解析上面的XML作为CSV文件,但我得到了如下结果.

脚本:

$xml = [xml](Get-Content .\monitoring_services.xml)
$xml.servstate.services.service |
    Select-Object @(@{l="name";e={$_.name."#text"}}, @{l="state";e={$_.state."#text"}}) |
    Export-Csv test.csv -NoTypeInformation

结果:

"name","state"
,
,
,
,
,
,

预期结果:

"name","state"
Oracle Weblogic AdminServer 1,RUNNING.
Oracle WebLogic NodeManager 1,RUNNING.
OracleProcessManager_1,RUNNING.
OracleProcessManager_2,RUNNING.
Oracle WebLogic NodeManager 2,RUNNING.
Oracle Weblogic AdminServer 1,RUNNING.

你能帮忙纠正脚本出了什么问题吗?

你为什么不用这个?

$xml.servstate.services.service | Export-Csv test.csv -NoTypeInformation
网友评论