如何使用php在csv中的不同行中将2个相似的子项转换为xml

时间:2016-05-21 17:29:18

标签: php xml csv

我有来自ebay Order API的xml名称shipped.xml,我正在挑战将它正确地转换为csv。例如,我们将有一段时间订购买家合并两件或更多件商品。通常他们会在同一行<Order>行。

我的挑战是如何将项目分成不同的行。每个项目的<TransactionID><SellingManagerSalesRecordNumber>通常不同,如何根据<TransactionID><SellingManagerSalesRecordNumber>生成csv。

这是xml示例

    <?xml version="1.0" encoding="UTF-8"?>
<GetOrdersResponse
    xmlns="urn:ebay:apis:eBLBaseComponents">
    <Timestamp>2016-05-21T09:07:54.497Z</Timestamp>
    <PaginationResult>
        <TotalNumberOfPages>1</TotalNumberOfPages>
        <TotalNumberOfEntries>94</TotalNumberOfEntries>
    </PaginationResult>
    <OrderArray>
        <Order>
            <OrderID>20934xxxxxxx</OrderID>
            <OrderStatus>Completed</OrderStatus>
            <CheckoutStatus>
                <Status>Complete</Status>
                <IntegratedMerchantCreditCardEnabled>false</IntegratedMerchantCreditCardEnabled>
            </CheckoutStatus>
            <ShippingDetails>
                <SalesTax>
                    <SalesTaxPercent>0.0</SalesTaxPercent>
                    <SalesTaxState></SalesTaxState>
                    <ShippingIncludedInTax>false</ShippingIncludedInTax>
                    <SalesTaxAmount currencyID="USD">0.0</SalesTaxAmount>
                </SalesTax>
                <ShippingServiceOptions>
                    <ShippingService>USPSFirstClass</ShippingService>
                    <ShippingServiceCost currencyID="USD">0.0</ShippingServiceCost>
                    <ShippingServicePriority>1</ShippingServicePriority>
                    <ExpeditedService>false</ExpeditedService>
                    <ShippingTimeMin>2</ShippingTimeMin>
                    <ShippingTimeMax>6</ShippingTimeMax>
                </ShippingServiceOptions>
                <SellingManagerSalesRecordNumber>15003</SellingManagerSalesRecordNumber>
                <GetItFast>false</GetItFast>
            </ShippingDetails>
            <CreatingUserRole>Buyer</CreatingUserRole>
            <CreatedTime>2016-05-20T15:49:56.000Z</CreatedTime>
            <PaymentMethods>PayPal</PaymentMethods>
            <SellerEmail>raaynike@yahoo.com</SellerEmail>
            <ShippingAddress>
                <Name>Ejoy. Wadeld</Name>
                <Street1>1611A S Zion Dr</Street1>
                <Street2># 134</Street2>
                <CityName>Vista</CityName>
                <StateOrProvince>CA</StateOrProvince>
                <Country>US</Country>
                <CountryName>United States</CountryName>
                <Phone>760 453 xxxx</Phone>
                <PostalCode>92081-5471</PostalCode>
                <AddressID>1488500000000</AddressID>
                <AddressOwner>eBay</AddressOwner>
                <ExternalAddressID></ExternalAddressID>
            </ShippingAddress>
            <ShippingServiceSelected>
                <ShippingService>USPSFirstClass</ShippingService>
                <ShippingServiceCost currencyID="USD">0.0</ShippingServiceCost>
            </ShippingServiceSelected>
            <Subtotal currencyID="USD">420.41</Subtotal>
            <Total currencyID="USD">420.41</Total>
            <ExternalTransaction>
                <ExternalTransactionID>0EF4715xxxxxxx</ExternalTransactionID>
                <ExternalTransactionTime>2016-05-20T15:49:56.000Z</ExternalTransactionTime>
                <FeeOrCreditAmount currencyID="USD">1.23</FeeOrCreditAmount>
                <PaymentOrRefundAmount currencyID="USD">42.41</PaymentOrRefundAmount>
            </ExternalTransaction>
            <TransactionArray>
                <Transaction>
                    <Buyer>
                        <Email>manraysxxx@hotmail.com</Email>
                        <UserFirstName></UserFirstName>
                        <UserLastName></UserLastName>
                    </Buyer>
                    <ShippingDetails>
                        <SellingManagerSalesRecordNumber>15001</SellingManagerSalesRecordNumber>
                    </ShippingDetails>
                    <CreatedDate>2016-05-20T15:49:56.000Z</CreatedDate>
                    <Item>
                        <ItemID>252285900000</ItemID>
                        <Site>US</Site>
                        <Title>Eternity Men By Calvin Klein- Etd Spray 3.4 Oz (Unboxed)</Title>
                        <SKU>xxxx-2099</SKU>
                        <ConditionID>1000</ConditionID>
                        <ConditionDisplayName>New</ConditionDisplayName>
                    </Item>
                    <QuantityPurchased>1</QuantityPurchased>
                    <Status>
                        <PaymentHoldStatus>None</PaymentHoldStatus>
                    </Status>
                    <TransactionID>1853810111111</TransactionID>
                    <TransactionPrice currencyID="USD">20.89</TransactionPrice>
                    <TransactionSiteID>US</TransactionSiteID>
                    <Platform>eBay</Platform>
                    <Taxes>
                        <TotalTaxAmount currencyID="USD">0.0</TotalTaxAmount>
                        <TaxDetails>
                            <Imposition>SalesTax</Imposition>
                            <TaxDescription>SalesTax</TaxDescription>
                            <TaxAmount currencyID="USD">0.0</TaxAmount>
                            <TaxOnSubtotalAmount currencyID="USD">0.0</TaxOnSubtotalAmount>
                            <TaxOnShippingAmount currencyID="USD">0.0</TaxOnShippingAmount>
                            <TaxOnHandlingAmount currencyID="USD">0.0</TaxOnHandlingAmount>
                        </TaxDetails>
                        <TaxDetails>
                            <Imposition>WasteRecyclingFee</Imposition>
                            <TaxDescription>ElectronicWasteRecyclingFee</TaxDescription>
                            <TaxAmount currencyID="USD">0.0</TaxAmount>
                        </TaxDetails>
                    </Taxes>
                    <ActualShippingCost currencyID="USD">0.0</ActualShippingCost>
                    <ActualHandlingCost currencyID="USD">0.0</ActualHandlingCost>
                    <OrderLineItemID>252285900000-1853810111111</OrderLineItemID>
                </Transaction>
                <Transaction>
                    <Buyer>
                        <Email>manraysxxxx@hotmail.com</Email>
                        <UserFirstName></UserFirstName>
                        <UserLastName></UserLastName>
                    </Buyer>
                    <ShippingDetails>
                        <SellingManagerSalesRecordNumber>15002</SellingManagerSalesRecordNumber>
                    </ShippingDetails>
                    <CreatedDate>2016-05-20T15:49:56.000Z</CreatedDate>
                    <Item>
                        <ItemID>351694900000</ItemID>
                        <Site>US</Site>
                        <Title>Love And Luck Men By Ed Hardy - Etd Spray 1.7 OZ</Title>
                        <SKU>xxxx-4702</SKU>
                        <ConditionID>1000</ConditionID>
                        <ConditionDisplayName>New</ConditionDisplayName>
                    </Item>
                    <QuantityPurchased>1</QuantityPurchased>
                    <Status>
                        <PaymentHoldStatus>None</PaymentHoldStatus>
                    </Status>
                    <TransactionID>800279444444</TransactionID>
                    <TransactionPrice currencyID="USD">210.52</TransactionPrice>
                    <TransactionSiteID>US</TransactionSiteID>
                    <Platform>eBay</Platform>
                    <ActualShippingCost currencyID="USD">0.0</ActualShippingCost>
                    <ActualHandlingCost currencyID="USD">0.0</ActualHandlingCost>
                    <OrderLineItemID>351694900000-800279444444</OrderLineItemID>
                </Transaction>
            </TransactionArray>
            <BuyerUserID>man-shopxyz</BuyerUserID>
            <PaidTime>2016-05-20T15:49:57.000Z</PaidTime>
            <IntegratedMerchantCreditCardEnabled>false</IntegratedMerchantCreditCardEnabled>
            <MonetaryDetails>
                <Payments>
                    <Payment>
                        <PaymentStatus>Succeeded</PaymentStatus>
                        <Payer type="eBayUser">man-shopxyz</Payer>
                        <Payee type="eBayUser">perfumeFx</Payee>
                        <PaymentTime>2016-05-20T15:49:56.000Z</PaymentTime>
                        <PaymentAmount currencyID="USD">42.41</PaymentAmount>
                        <ReferenceID type="ExternalTransactionID">0EF471563vvvvvvvv</ReferenceID>
                        <FeeOrCreditAmount currencyID="USD">1.23</FeeOrCreditAmount>
                    </Payment>
                </Payments>
            </MonetaryDetails>
        </Order>
    </OrderArray>
    <OrdersPerPage>100</OrdersPerPage>
    <PageNumber>1</PageNumber>
    <ReturnedOrderCountActual>94</ReturnedOrderCountActual>
</GetOrdersResponse>

这是我的脚本,它在订单商品订单上运作正常。但不是多重,请在此建议我

$filexml='shipped.xml';
if (file_exists($filexml)) {
  $xml = simplexml_load_file($filexml);
  $f = fopen('product.csv', 'w');


$headers = ['Sales Record Number','Sales Record Number2','User Id ','Buyer Fullname ','Buyer Phone Number ','Buyer Email ','Buyer Address 1 ','Buyer Address 2 ','Buyer City ','Buyer State ','Buyer Zip ','Buyer Country ','Order ID ','Item ID ','Transaction ID ','Item Title ','Quantity ','Sale Price ','Shipping And Handling ','Total Price ','Notes to Yourself ','Custom Label','Shipped Time'];

fputcsv($f, $headers);


foreach($xml->OrderArray->Order as $product) {        


if ($product->ShippedTime != ""){

continue;

}

  $values = array(

   "Sales Record Number" => $product->TransactionArray->Transaction->ShippingDetails->SellingManagerSalesRecordNumber,
   "Sales Record Number2" => $product->ShippingDetails->SellingManagerSalesRecordNumber,
"User Id" => $product->BuyerUserID,
"Buyer Fullname" => $product->ShippingAddress->Name,
"Buyer Phone Number" => $product->ShippingAddress->Phone,
"Buyer Email" => $product->TransactionArray->Transaction->Buyer->Email,
"Buyer Address 1" => $product->ShippingAddress->Street1,
"Buyer Address 2" => $product->ShippingAddress->Street2,
"Buyer City" => $product->ShippingAddress->CityName,
"Buyer State" => $product->ShippingAddress->StateOrProvince,
"Buyer Zip" => $product->ShippingAddress->PostalCode,
"Buyer Country" => $product->ShippingAddress->CountryName,
"Order ID" => $product->OrderID,
"Item ID" => $product->TransactionArray->Transaction->Item->ItemID,
"Transaction ID" => $product->TransactionArray->Transaction->TransactionID,
"Item Title" => $product->TransactionArray->Transaction->Item->Title,
"Quantity" => $product->TransactionArray->Transaction->QuantityPurchased,
"Sale Price" => $product->->TransactionArray->Transaction->TransactionPrice,
"Shipping And Handling" => $product->ShippingServiceSelected->ShippingService,
"Total Price" => $product->TransactionArray->Transaction->TransactionPrice,
"Notes to Yourself" => $product->BuyerCheckoutMessage,
"Custom Label" => $product->TransactionArray->Transaction->Item->SKU,
"Shipped Time" => $product->ShippedTime);
   fputcsv($f, $values,',','"');
  }


fclose($f);
}

0 个答案:

没有答案