I want to export product from magento in xml file. I don't know how to write a xml file using magento lib. Please assist me for this.


1 Answer 1


You don't need to write the xml using the magento lib.
Just make it a simple string.

$collection = Mage::getModel('catalog/product')->getCollection();
//add filters if needed.
//$collection->addAttributeToFilter('status', 1); //for example.

$padd = '    '; //4 spaces for identation
$eol = "\n"; //end of line
$xml = '<?xml version="1.0"?>'.$eol;
$xml .= '<products>'.$eol;
foreach ($collection as $product) {
    $xml .= $padd.'<product>'.$eol;
    $xml .= str_repeat($padd, 2).'<name>'.$product->getName().'</name>'.$eol;
    $xml .= str_repeat($padd, 2).'<description><![CDATA['.$product->getName().']]></description>'.$eol;
    $xml .= str_repeat($padd, 2).'<price><![CDATA['.$product->getFinalPrice().']]></price>'.$eol;
    //add here all the attributes you need to export
    $xml .= $padd.'</product>'.$eol;
$xml .= '</products>'.$eol;
//Do something with $xml - save it in a file or echo it.

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.