I want to create a product XML file like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

How can this be done?

2 Answers 2


Based on: https://gist.github.com/Nolwennig/b67bbbcba359c04fe77df9393dc3907a

You can put this script in root directory, aside of app/

if(php_sapi_name() != 'cli')
    die('only via cli');
require 'app/Mage.php';
ini_set('display_errors', 'on');
$resource = Mage::getSingleton('core/resource');
/* @var $resource Mage_Core_Model_Resource */
$conn = $resource->getConnection('core_write');
/* @var $conn Varien_Db_Adapter_Interface */
$dir = Mage::getBaseDir().DS.'export';
    mkdir($dir, 0755, true);
$newFile = $dir . DS . 'product.xml';
$tmpFile = $newFile . '.tmp';
$fp = fopen($tmpFile, 'w');
fwrite($fp, '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'."\n");
fwrite($fp, '<root>'."\n");
fwrite($fp, '<stocks>'."\n");
$collection = Mage::getModel('catalog/product')->getCollection();
foreach($collection as $_product)
    fwrite($fp, '<product>'."\n");
    fwrite($fp, '  <sku><![CDATA['.$_product->getSku().']]></sku>'."\n");
    fwrite($fp, '  <eansku><![CDATA['.$_product->getEansku().']]></eansku>'."\n");
    fwrite($fp, '  <stock>'.$_product->getQty().'</stock>'."\n");
    fwrite($fp, '</product>'."\n");
fwrite($fp, '</stocks>'."\n");
fwrite($fp, '</root>'."\n");
rename($tmpFile, $newFile);

I write this quickly, without test and just a quick final checking. Test and review before run that.

  • where i put this file in magento ?
    – Piyush
    Commented Nov 14, 2017 at 10:58
  • in root directory, aside of app/
    – Nolwennig
    Commented Nov 14, 2017 at 14:35
  • Can you please explain how it work ?
    – Piyush
    Commented Nov 15, 2017 at 4:35
  • Get the 39 lines of code and paste this in a file. Call your file product.xml_generator.php, for example. After that, put the file on your magento server, to run it.
    – Nolwennig
    Commented Nov 15, 2017 at 14:26

Please follow these steps:

Get the list of products you want to include in the XML file. For example, if you want to include all enabled products, then:

$products = Mage::getModel('catalog/product')->getCollection()
    array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED)

Then loop through all products and put the required attributes in the XML file.

Note: To know how to create XML file programmatically, please check https://stackoverflow.com/questions/486757/how-to-generate-xml-file-dynamically-using-php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.