将 XML 解析为 Table,其中值可能不存在

Parse XML to Table Where Value May Not Exist

我正在尝试找出一种将 XML 文件中的数据解析为 table 的方法。问题是,如果 ChargeType 在 XML 字段中不一致,我将无法在 table 中正确显示它。

这是 XML:

<ItemTaxWithheldList>
    <TaxWithheldComponent>
        <TaxesWithheld>
            <ChargeComponent>
                <ChargeType>MarketplaceFacilitatorTax-Shipping</ChargeType>
                <ChargeAmount>
                    <CurrencyAmount>-5.54</CurrencyAmount>
                </ChargeAmount>
            </ChargeComponent>
            <ChargeComponent>
                <ChargeType>MarketplaceFacilitatorTax-Principal</ChargeType>
                <ChargeAmount>
                    <CurrencyAmount>-10.87</CurrencyAmount>
                </ChargeAmount>
            </ChargeComponent>
        </TaxesWithheld>
    </TaxWithheldComponent>
    <TaxWithheldComponent>
        <TaxesWithheld>
            <ChargeComponent>
                <ChargeType>MarketplaceFacilitatorTax-Other</ChargeType>
                <ChargeAmount>
                    <CurrencyAmount>-0.27</CurrencyAmount>
                </ChargeAmount>
            </ChargeComponent>
            <ChargeComponent>
                <ChargeType>MarketplaceFacilitatorTax-Shipping</ChargeType>
                <ChargeAmount>
                    <CurrencyAmount>0.0</CurrencyAmount>
                </ChargeAmount>
            </ChargeComponent>
            <ChargeComponent>
                <ChargeType>MarketplaceFacilitatorTax-Principal</ChargeType>
                <ChargeAmount>
                    <CurrencyAmount>0.0</CurrencyAmount>
                </ChargeAmount>
            </ChargeComponent>
        </TaxesWithheld>
    </TaxWithheldComponent>
    <TaxWithheldComponent>
        <TaxesWithheld>
            <ChargeComponent>
                <ChargeType>MarketplaceFacilitatorTax-Shipping</ChargeType>
                <ChargeAmount>
                    <CurrencyAmount>0.0</CurrencyAmount>
                </ChargeAmount>
            </ChargeComponent>
            <ChargeComponent>
                <ChargeType>MarketplaceFacilitatorTax-Principal</ChargeType>
                <ChargeAmount>
                    <CurrencyAmount>-4.87</CurrencyAmount>
                </ChargeAmount>
            </ChargeComponent>
        </TaxesWithheld>
    </TaxWithheldComponent>
</ItemTaxWithheldList>

这是我的代码:

<!DOCTYPE html>
<html>
<head>
<style>
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
</style>
</head>
<body>
<table style="width:100%">
  <tr>
    <th>MarketplaceFacilitatorTax-Other</th>
    <th>MarketplaceFacilitatorTax-Shipping</th>
    <th>MarketplaceFacilitatorTax-Principal</th>
</tr>
<?php
$response = $xmlFile;

$return_data = simplexml_load_string($response);
$array = json_decode(json_encode((array)$return_data), TRUE);

foreach ($array['TaxWithheldComponent'] as $itemTaxWithheldList) {
    
echo '<tr>';

    foreach ($itemTaxWithheldList['TaxesWithheld']['ChargeComponent'] as $key) {
        $mpftChargeType = $key['ChargeType'];
        $mpftChargeAmount = number_format((float)$key['ChargeAmount']['CurrencyAmount'], 2, '.', '');

        
        if ($mpftChargeType == 'MarketplaceFacilitatorTax-Other') {
            echo '<td>' . $mpftChargeAmount . '</td>';
        } else {
            echo '<td>0.00</td>';
        } 
        
        if ($mpftChargeType == 'MarketplaceFacilitatorTax-Shipping') {
            echo '<td>' . $mpftChargeAmount . '</td>';
        } else {
            echo '<td>0.00</td>';
        }  
        
        if ($mpftChargeType == 'MarketplaceFacilitatorTax-Principal') {
            echo '<td>' . $mpftChargeAmount . '</td>';
        } else {
            echo '<td>0.00</td>';
        }  

    }

echo '</tr>';

}

?>
</table></body></html>

输出完全错误,因为我只需要每个列和行的三列:

如果有人能指出正确的方向,我将不胜感激。

您可以通过将 XML 中找到的费用存储到每个项目的数组中,然后迭代每种费用类型并输出值(如果存在)或 0.00否则:

$chargeTypes = array('MarketplaceFacilitatorTax-Other', 'MarketplaceFacilitatorTax-Shipping', 'MarketplaceFacilitatorTax-Principal');
foreach ($array['TaxWithheldComponent'] as $itemTaxWithheldList) {
    $charges = array();
    foreach ($itemTaxWithheldList['TaxesWithheld']['ChargeComponent'] as $key) {
        $mpftChargeType = $key['ChargeType'];
        $mpftChargeAmount = number_format((float)$key['ChargeAmount']['CurrencyAmount'], 2, '.', '');
        $charges[$mpftChargeType] = $mpftChargeAmount;
    }
    echo '<tr>';
    foreach ($chargeTypes as $chargeType) {
        echo '<td>' . ($charges[$chargeType] ?? '0.00') . '</td>';
    }
    echo '</tr>';
}

输出(对于您的示例数据):

<tr><td>0.00</td><td>-5.54</td><td>-10.87</td></tr>
<tr><td>-0.27</td><td>0.00</td><td>0.00</td></tr>
<tr><td>0.00</td><td>0.00</td><td>-4.87</td></tr>

Demo on 3v4l.org