使用循环或直接访问解析嵌套的xml simplexml_load_string / simple xml对象

时间:2017-08-02 10:06:21

标签: php xml xml-parsing simplexml simplexml-load-string

我是PHP的新手。我有以下XML文件。我想解析组件。如您所见,此处重复了组件标记。我正在使用simplexml_load_string来获取HTTP请求的响应。此处重复此XML架构(<order> </order>)在此处重复。我想解析所有使用循环或直接访问。

<Order>
    <AmazonOrderID>406-6143419-7223518</AmazonOrderID>
    <MerchantOrderID>406-6143419-7223518</MerchantOrderID>
    <ShipmentID>DDNSkvgJN</ShipmentID>
    <MarketplaceName>Amazon.in</MarketplaceName>
    <Fulfillment>
        <MerchantFulfillmentID>AFN</MerchantFulfillmentID>
        <PostedDate>2017-07-22T08:31:18+00:00</PostedDate>
        <Item>
            <AmazonOrderItemCode>31251963544243</AmazonOrderItemCode>
            <SKU>DZ-HA0T-A5GQ</SKU>
            <Quantity>1</Quantity>
            <ItemPrice>
                <Component>
                    <Type>Principal</Type>
                    <Amount currency="INR">780.47</Amount>
                </Component>
                <Component>
                    <Type>Shipping</Type>
                    <Amount currency="INR">13.02</Amount>
                </Component>
                <Component>
                    <Type>Tax</Type>
                    <Amount currency="INR">174.78</Amount>
                </Component>
            </ItemPrice>
            <ItemFees>
                <Fee>
                    <Type>FBAPerUnitFulfillmentFee</Type>
                    <Amount currency="INR">-10.00</Amount>
                </Fee>
                <Fee>
                    <Type>FBAPerUnitFulfillmentFeeTax</Type>
                    <Amount currency="INR">-1.80</Amount>
                </Fee>
                <Fee>
                    <Type>FBAWeightBasedFee</Type>
                    <Amount currency="INR">-43.00</Amount>
                </Fee>
                <Fee>
                    <Type>FBAWeightBasedFeeTax</Type>
                    <Amount currency="INR">-7.74</Amount>
                </Fee>
                <Fee>
                    <Type>Commission</Type>
                    <Amount currency="INR">-135.83</Amount>
                </Fee>
                <Fee>
                    <Type>CommissionTax</Type>
                    <Amount currency="INR">-24.45</Amount>
                </Fee>
                <Fee>
                    <Type>FixedClosingFee</Type>
                    <Amount currency="INR">-20.00</Amount>
                </Fee>
                <Fee>
                    <Type>FixedClosingFeeTax</Type>
                    <Amount currency="INR">-3.60</Amount>
                </Fee>
            </ItemFees>
            <Promotion>
                <MerchantPromotionID>DCMS-3f4089f9-060b2c1e</MerchantPromotionID>
                <Type>Principal</Type>
                <Amount currency="INR">-156.25</Amount>
            </Promotion>
            <Promotion>
                <MerchantPromotionID>IN Core Free Shipping 2015/04/08 23-48-5-108</MerchantPromotionID>
                <Type>Shipping</Type>
                <Amount currency="INR">-13.02</Amount>
            </Promotion>
        </Item>
    </Fulfillment>
</Order>

到目前为止,我已尝试过此代码:

$xml = simplexml_load_string($xmlDoc); 
foreach($xml->children() as $node) { 
  echo $node->AmazonOrderID . ", ";
  echo $node->ShipmentID . ", "; 
  echo $node->SKU . ", "; 
  echo $node->Component . "<br>"; 
}

2 个答案:

答案 0 :(得分:1)

我已将数据放入文件中,但这是对加载位的简单更改(我使用_file,而不是_string)但其余代码应该向您展示如何访问数据的层次结构。 ..

$xml = simplexml_load_file($url);
echo $xml->AmazonOrderID . ", ";
echo $xml->ShipmentID . ", ";
echo $xml->SKU . ", ";
foreach($xml->Fulfillment->Item as $item) {
    foreach ( $item->ItemPrice->Component as $component )   {
        echo "Type:".$component->Type." Amount:".$component->Amount.PHP_EOL;
    }
}

哪个给你......

406-6143419-7223518, DDNSkvgJN, , Type:Principal Amount:780.47
Type:Shipping Amount:13.02
Type:Tax Amount:174.78

答案 1 :(得分:0)

您可以使用xpath方法轻松访问Component元素。

$components = $xml->xpath('Fulfillment/Item/ItemPrice/Component');
print_r($components);

那给你这个数组:

Array
(
    [0] => SimpleXMLElement Object
        (
            [Type] => Principal
            [Amount] => 780.47
        )

    [1] => SimpleXMLElement Object
        (
            [Type] => Shipping
            [Amount] => 13.02
        )

    [2] => SimpleXMLElement Object
        (
            [Type] => Tax
            [Amount] => 174.78
        )

)