PHP Magento Tips

How to generate XML file dynamically using PHP?

Rate this posting:
{[['']]}

You can easily create your XML Configuration file dynamically with PHP script by using SimpleXMLElement.

In below example, I created one simple xml file to create simple config.xml file for one of Magento WebRotate 360 Product Viewer extension.

Create a file  scriptxml.php and add below code in it.

<?php



$sxe = new SimpleXMLElement('<xml/>');



$sxe->addAttribute('version', '1.0');

$sxe->addAttribute('encoding', 'utf-8');



$movie = $sxe->addChild('config');



$settings = $movie->addChild('settings', ' ');

$hotspot = $movie->addChild('hotspots', ' ');



$loader = $settings->addChild('preloader');

$loader->addAttribute('image', 'images/MyImage_2000.png'); //define your preloaded image here



// user interface configuration

$userinterface = $settings->addChild('userInterface');

$userinterface->addAttribute('showZoomButtons', 'true');

$userinterface->addAttribute('showHotspotsButton', 'true');

$userinterface->addAttribute('showFullScreenButton', 'false');

$userinterface->addAttribute('showTogglePlayButton', 'true');

$userinterface->addAttribute('showArrows', 'true');

$userinterface->addAttribute('toolbarAlign', 'center');

$userinterface->addAttribute('toolbarBackColor', '#FFFFFF');

$userinterface->addAttribute('toolbarHoverColor', '#808285');

$userinterface->addAttribute('toolbarForeColor', '#A7A9AE');

$userinterface->addAttribute('toolbarBackAlpha', '0.9');

$userinterface->addAttribute('toolbarAlpha', '1');

$userinterface->addAttribute('flashEmbedFonts', 'true');

$userinterface->addAttribute('progressLoopColor', '#e8e8e8');

$userinterface->addAttribute('progressNumColor', '#949494');



// control configuration

$control = $settings->addChild('control');

$control->addAttribute('dragSpeed', '0.13');

$control->addAttribute('maxZoom', '200');

$control->addAttribute('inBrowserFullScreen', 'true');

$control->addAttribute('maxZoomFullScreen', '200');

$control->addAttribute('fullScreenStretch', '100');

$control->addAttribute('doubleClickZooms', 'true');

$control->addAttribute('disableMouseControl', 'false');

$control->addAttribute('reverseScroll', 'false');

$control->addAttribute('hideHotspotsOnLoad', 'false');



// rotation configuration

$rotation = $settings->addChild('rotation');

$rotation->addAttribute('firstImage', '33'); //define your first image here

$rotation->addAttribute('rotate', 'false');

$rotation->addAttribute('rotatePeriod', '7');

$rotation->addAttribute('rotateDirection', '-1');

$rotation->addAttribute('bounce', 'false');

$rotation->addAttribute('useInertia', 'true');





$images = $movie->addChild('images');

$images->addAttribute('highresWidth', '1200'); //define High resolution image width

$images->addAttribute('highresHeight', '791'); //define High resolution image height





for ($i = 1; $i <= 35; ++$i) { //define number of images for loop.

 $image  = $images->addChild('image', ' ');

 $image->addAttribute('src', "images/MyImage_200$i.png");

 $highresimage  = $image->addChild('highres');

 $highresimage->addAttribute('src', "images/MyImage_200$i.png");

}



Header('Content-type: text/xml');

//header('Content-Disposition: attachment; filename="config.xml"'); // this code open file as download.

//echo $sxe->asXML();



print($sxe->asXML());

Run above script in your browser and find your XML file ready in just one click !!
Tag : Magento
0 Comments On "How to generate XML file dynamically using PHP?"

Back To Top