将XML转换为CSV不起作用

时间:2018-06-29 07:54:49

标签: xml powershell csv

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.

您能帮助纠正脚本出了什么问题吗?

2 个答案:

答案 0 :(得分:6)

你为什么不只使用这个?

$xml.servstate.services.service | Export-Csv test.csv -NoTypeInformation

答案 1 :(得分:0)

为什么要使事情复杂化?为什么不选择名称和州,然后将其导出为CSV:

 /**
 * @Route("/edit/{id}", name="billing_setting_edit")
 */
public function editAction(Request $request, BillingSetting $billinSetting)
{
    $entityManager = $this->getDoctrine()->getManager();
    $oldName = $billinSetting->getPricing()->getName();
    $oldMinPrice = $billinSetting->getPricing()->getMinPrice();
    $oldUnnecessaryDisplacements = $billinSetting->getPricing()->getUnnecessaryDisplacements();
    $oldDiscountByEquipement = $billinSetting->getPricing()->getDiscountByEquipement();
    $oldMaxEquipmentDiscount = $billinSetting->getPricing()->getMaxEquipmentDiscount();

    $editForm = $this->createForm(BillingSettingType::class, $billinSetting);
    $editForm->handleRequest($request);

    if ($editForm->isValid()) {
        $entityManager->flush();
        $message = $this->get('translator')->trans('billing_settings.update.update_billing_setting');
        $this->get('session')->getFlashBag()->set('success', $message);
        // Force doctrine to refresh object
        $entityManager->refresh($billinSetting);
        // For updating Elasticsearch data
        ...

        return $this->redirectToRoute('billing_setting_list');
    }

    return $this->render(
        '@App/page/billingSettings/edit.html.twig',
        [
            'billingSetting' => $billinSetting,
            'form' => $editForm->createView(),
            'missionTypeLabels' => $this->getLabels()['missionTypeLabels'],
            'pricingMissionTypeEquipmentTypeLabels' => $this->getLabels()['pricingMissionTypeEquipmentTypeLabels'],
        ]
    );
}

如果您已经拥有所需的内容,则无需创建自定义属性。