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'.
I figured out how to do it. I had to really break down simplexml to understand how the object layers so that I could figure out how to extract the data I wanted. Hopefully, this makes sense to someone, but when you're trying to extract a particular value from a SimpleXML object, you want it to read like this when you print_r():
SimpleXMLElement Object
(
[0] => 0 //0 being the value you want - you can now add this to whatever variable you want once you have it down to this.
)
Here was the code that did it:
//Iterate through the first node <OrderItem>
foreach ($OrderItem as $n) {
//Add the AmazonOrderId to the start of each line
foreach ($AmazonOrderId as $t) {
$row[]=$t;
}
$child = $xml->xpath('//a:OrderItem['.$i.']/*');
// Iterate through each child of <OrderItem> node
foreach ($child as $value) {
//If the child has an attribute named <Amount>
if($value->Amount[0]){
//Iterate through and grab the value of <Amount> and post add to $row
foreach ($value->Amount as $t) {
$row[] = $t;
}
}
//Else add the value to $row
else {
$row[] = $value;
}
}
$fs = fopen('141.csv', 'a');
fputcsv($fs, $row);
$row = [];
$i++;
}
fclose($fs);