2017-08-02 14 views
-1

私はPHPの初心者です。私は以下のXMLファイルを持っています。私はコンポーネントを解析したいと思います。あなたが見ることができるように、コンポーネントタグはここで繰り返されます。私はsimplexml_load_stringを使用してHTTPリクエストからの応答を取得しています。このXMLスキーマはここで繰り返されます(<order></order>)。私はすべてのループまたは直接アクセスを使用して解析したい。ループまたは直接アクセスを使用してネストされたxml simplexml_load_string/simple 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マニュアルにはこのような単純なXML文書を解析するのに十分なことが分かるはずの[単純な例のページ](http://php.net/manual/en/simplexml.examples-basic.php)があります。 – IMSoP

答えて

0

あなたはXPathの方法で簡単に構成要素にアクセスすることができます。

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'を許可しないので' new SimpleXMLElement'を使用しました。 – nanocv

1

は、私は、ファイルにデータを入れているが、これは、単純な負荷ビットへの変更(私は_stringない、_file使用)しかし、次のとおりです。あなたにこの配列を与える

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

コードの残りのあなたを与える

$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 
関連する問題