0

I have a ecommerce site that needs to update products via a huge XML file downloaded via ftp from the distributor.

The products.xml file is approx 21MB in size and pulling the data and looping through it is simply too exhaustive for a single pass.

I need to be able to split the original XML file or my processed SimpleXMLElement into 1000 products each, then load the files and process them separately.

I'm pulling the xml into a SimpleXMLElement then casting it to an array so it looks like so:

[1] => Array
    (
        [PRODUCTS_NAME] => Product Name
        [PRODUCTS_DESCRIPTION] => Product Description
        [PRODUCTS_IMAGE] => 1234.jpg
        [PRODUCTS_PRICE] => 9.99
        [PRODUCTS_MODEL] => 1234
        [ITEM_BRANDNAME] => Acme Inc.
        [ITEM_UPC] => 01234567890
        [ITEM_HEIGHT] => 0.500
        [ITEM_LENGTH] => 4.250
        [ITEM_DIAMETER] => 3.375
        [PRODUCTS_WEIGHT] => 0.05
        [MANUFACTURERS_NAME] => Acme Distributors
        [ITEM_VENDOR_NUMBER] => XXX-1234
        [PRODUCTS_QUANTITY] => 100
        [DATE_RECIEVED] => 0000-00-00 00:00:00
        [PROP_PACKAGING] => Blister Card
        [PRODUCT_CLASS] => BAT
        [PRODUCTS_TYPE] => Batteries
    )

How can I create individual files of 1000 products in each file and save them?

secondman
  • 3,233
  • 6
  • 43
  • 66
  • I know it's tangential to the question, but why are you casting the object into an array? The whole point of SimpleXML is that you can access it like an object or array and pull out whichever details you want. – IMSoP Aug 26 '13 at 16:48
  • do you have to use PHP? is Java ok with u? – vtd-xml-author Aug 26 '13 at 20:10

2 Answers2

3

You can do this with simplexml functions:

$xml = simplexml_load_file('products.xml');

foreach($xml as $id => $product)
{
    $product->asXml($id.'.xml');
}

You can use simplexml_load_string instead of simplexml_load_file

$xml = simplexml_load_string($products);

Note: Have to be SimpleXMLElement

Bora
  • 10,529
  • 5
  • 43
  • 73
0

Switched to XMLReader with XMLReaderIterator

and used the instructions here.

Really nice implementation, thanks to hakre for building that, works awesome.

Community
  • 1
  • 1
secondman
  • 3,233
  • 6
  • 43
  • 66