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

问题描述:

我是PHP的新手。我有以下XML文件。我想解析组件。正如你可以看到组件标签在这里重复。我正在使用simplexml_load_string来获取来自HTTP请求的响应。这里重复这个XML模式(<order></order>)在这里重复。我想解析所有使用循环或直接访问。使用循环或直接访问解析嵌套的xml simplexml_load_string /简单的xml对象

<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>"; 
} 
+0

你有什么尝试?我们不会为您编写代码。 – Flummox

+0

PHP手册有一个[简单示例页面](http://php.net/manual/en/simplexml.examples-basic.php),它应该足以让您解析这样一个简单的XML文档。 – IMSoP

您可以使用XPath方法方便地访问组件的元素。

$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 
     ) 

) 
+0

用xpath试过不行。不工作:( –

+0

你可以看到它在这里工作:http://sandbox.onlinephpfunctions.com/code/93c16986fc85a03fc20804a2f30ae4ae570ac461他们不允许'simplexml_load_string',所以我用'新的SimpleXMLElement'。 – nanocv

我已经把数据写入文件,但这是一个简单的变化负载位(我用_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