How to Export Magento Products in XML File
XML surely offers advantages over CSV that makes it more preferable for information exchange. Owing to its flexibility, object-oriented characteristic, protection against invalid data insertion, hierarchical nature and support for Unicode implicitly; it is the first choice to export data in Magento.
Magento Export is a useful feature required to create data feeds, send data to multiple people, backup product data, implement changes or import data to other websites. The default Magento only allows CSV or MS Excel file to export product data. To overcome the limitation and to be able to use XML file format, one needs to implement custom code given here!
Implement the below method to export Magento products in XML file. Make the migration of your store flawless by using XML file format for the import-export of the data.
Method to Export Magento Products in XML File:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<?php require 'app/Mage.php'; Mage::app(); $storename = 'default'; $file = "products-top10.xml"; if (file_exists($file)) { unlink($file); } try { $products = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSelect('*') ->setPageSize(200) ->setCurPage(1) ->setOrder('id', 'ASC') ->addAttributeToFilter('status', array('eq' => '1')); $doc = new DOMDocument(); $doc->encoding = 'utf-8'; $doc->formatOutput = true; $root = $doc->createElement("root"); $doc->appendChild($root); $productsX = $doc->createElement("catalog"); $root->appendChild($productsX); foreach ($products as $_product) { $product = $doc->createElement("product"); $id = $doc->createElement("id"); $id->appendChild( $doc->createTextNode($_product->getId()) ); $product->appendChild($id); $url = $doc->createElement("url"); $url->appendChild( $doc->createTextNode(trim($_product->getData('url_key'))) ); $product->appendChild($url); $urlPath = $doc->createElement("url_path"); $urlPath->appendChild( $doc->createTextNode(trim($_product->getProductUrl())) ); $product->appendChild($urlPath); $title = $doc->createElement("title"); $title->appendChild( $doc->createTextNode(trim($_product->getName())) ); $product->appendChild($title); $sku = $doc->createElement("sku"); $sku->appendChild( $doc->createTextNode($_product->getSku()) ); $product->appendChild($sku); $price = $doc->createElement("price"); $price->appendChild( $doc->createTextNode(trim((int)$_product->getPrice())) ); $product->appendChild($price); $formatedprice = $doc->createElement("formated_price"); $formattedPrice = Mage::helper('core')->currency($_product->getPrice(), true, false); $formatedprice->appendChild( $doc->createTextNode(trim($formattedPrice)) ); $product->appendChild($formatedprice); $productsX->appendChild($product); } file_put_contents($file, $doc->saveXML(), FILE_APPEND); } catch (Exception $e) { echo 'Eroror : - '; echo $e->getMessage(); } |
With this method, the admin can export Magento products and its data in XML file easily.
Post any doubts in the comments section and I’d be happy to help 🙂
Reward the post with 5 stars if your task was made easy!
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
12 Comments
Super script thank you. Can we export attribute values? We tried everything and nothing works please help us.
Hey Matika,
For that you have to export from eav_attribute_value table.
Dear friend thank you for the script, is there a way to filter out only visible product and ignore others?
Hey Boky,
You can add filter on collection like this:
$productCollection->addFieldToFilter(‘visibility’, Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
Hello,
I have saved the code in a files called exportxml.php and added to pub with file permission 777. I am not sure what to do next. Please advice
Hello Ana,
Please be informed that the above code is for Magento 1. So make sure that you’re not trying to use it in Magento 2,
In order to run this file, use websiteurl/filename.php in the browser
Thank You
Hi Sanjay
Can you please help me with the integration. Where are these files? Thank you
Hello Phil,
This is the root script. So place it in Magento’s root folder.
Thank you.
Very usefull thanks
If we want the category path in the xml?
Thanks,
Here you go:
categoryIds = $_product->getCategoryIds();
$catUrl = ”;
foreach ($categoryIds as $category_id) {
$_cat = Mage::getModel(‘catalog/category’)->setStoreId(Mage::app()->getStore()->getId())->load($category_id);
$catUrl = $_cat->getUrl();
}
$caturls = $doc->createElement(“caturl”);
$caturls->appendChild(
$doc->createTextNode(trim($catUrl))
);
$product->appendChild($caturls);
Hello!
What do we have to add if we want and the image url?
Use the code below to export images:
$images = $doc->createElement("images");
//$proImage = Mage::helper('catalog/image')->init($_product, 'image');
$proImage = Mage::getBaseUrl() . 'media/catalog/product' . $_product->getThumbnail();
$images->appendChild(
$doc->createTextNode(trim($proImage))
);
$product->appendChild($images);