I've been trying to parse this data for a while now, but I don't really understand simpleXML and how it stores/retrieves. I have the following xml:
<ListOrderItemsResult>
<OrderItems>
<OrderItem>
<QuantityOrdered>1</QuantityOrdered>
<Title>Organic Chamomile & Lavender Shea Butter CP Soap Making Kit 2 Lbs.</Title>
<ItemPrice>
<CurrencyCode>USD</CurrencyCode>
<Amount>29.99</Amount>
</ItemPrice>
<ItemTax>
<CurrencyCode>USD</CurrencyCode>
<Amount>0.00</Amount>
</ItemTax>
</OrderItem>
<OrderItem>
<QuantityOrdered>1</QuantityOrdered>
<Title>Eucalyptus & Mint Organic Shea Butter CP Soap Making Kit 3 lbs.</Title>
<ItemPrice>
<CurrencyCode>USD</CurrencyCode>
<Amount>32.99</Amount>
</ItemPrice>
<ItemTax>
<CurrencyCode>USD</CurrencyCode>
<Amount>0.00</Amount>
</ItemTax>
</ShippingDiscount>
</OrderItem>
</OrderItems>
<AmazonOrderId>12134</AmazonOrderId>
</ListOrderItemsResult>
I'm trying to convert it into csv with each OrderItem being a row. The columns are: QuantityOrdered, Title, ItemPrice, ItemTax. So far I've managed to grab the everything but ItemPrice and ItemTax, because they have children nodes and I have no idea how to access them.
Here is my code:
$OrderItem = $xmlDocs->xpath('//a:OrderItem');
foreach ($OrderItem as $n) {
foreach ($AmazonOrderId as $t) {
$row[]=$t;
}
// Iterate through each child of <OrderItem> node
$child = $xml->xpath('//a:OrderItem['.$i.']/*');
foreach ($child as $value) {
$row[] = $value;
}
print_r($row);
$fs = fopen('141.csv', 'a');
fputcsv($fs, $row);
$row = [];
// Clean out array for next <item> (i.e., row)
$i++; // Move to next <item> (i.e., node position)
}
fclose($fs);
I have it working, but it's since I can't figure out how to get to the child of the child, nothing shows up for ItemPrice and ItemTax.
TL;DR - I'm trying to cycle through each OrderItem node and get the values of each child node and if the child node has children, I only want to get the value of the node named 'Amount'.