I need to generate an .xml file from my extension. Inside Namespace/Module/view/adminhtml/ui_component/ folder,

I need to do this pro-grammatically because the .xml file will be generated based on data collection, is there a way to do this?

  • 1
    it's never a good idea to generate the layout files on the fly. It screws up your versioning. Explain the problem you are trying to solve instead of the approach you've taken. Maybe there is a different way of solving it without generating ui-components file.
    – Marius
    Commented Jul 10, 2017 at 9:18
  • @Marius it related with my other question basically, i need to add custom column in sales order grid dynamically Commented Jul 10, 2017 at 9:21

2 Answers 2


For now i'm using php original function to write/create file inside my extension directory like this:

    public function __construct(
      \Magento\Framework\Module\Dir\Reader $moduleReader
        $this->customAttribute = $customAttribute;
        $baseDir = $moduleReader->getModuleDir('', 'Namespace_Module');
        $this->dir = $baseDir . '/view/adminhtml/ui_component';

    public function writeFile() {
        $dir = $this->dir;
        $fileName = 'test.xml';
        $content =  '<test>Test</test>';

        $myfile = fopen($dir . '/' . $fileName, "w") or die("Unable to open file!");
        try {
          fwrite($myfile, $content);
        } catch (Exception $e) {

if there's more proper way to do it in Magento 2 please let me know, and i will accept the answer for this question, but for now if anyone want to use this as a solution it's working properly for me but i do not recommend it

  • That is a really bad practice - using native PHP functions eliminates the advantage of using Magento's built-in framework. I would suggest taking a look at core modules, especially import/export ones.
    – Bery
    Commented Mar 21, 2018 at 13:07

If you would like to try another way, maybe use Magento\Framework\Filesystem\Io\File and Magento\Framework\Convert\ConvertArray. ConvertArray is useful to make an xml file from a multidimensional array and File can write it for you (and check permissions, create directories and more). Here is a basic example:

public function __construct(
    \Magento\Framework\Filesystem\Io\File $file,
    \Magento\Framework\Convert\ConvertArray $convertArray
    $this->file = $file;
    $this->convertArray = $convertArray;

public function createMyXmlFile($assocArray, $rootNodeName, $filename = 'file.xml')
   // ConvertArray function assocToXml to create SimpleXMLElement
   $simpleXmlContents = $this->convertArray->assocToXml($assocArray,rootNodeName);
   // convert it to xml using asXML() function
   $content = $simpleXmlContents->asXML();
   $this->file->write($filename, $contents);

if my array is :

$myArray = array(
  'fruit' => 'apple',
  'vegetables' => array('vegetable_1' => 'carrot', 'vegetable_2' => 'tomato'),
  'meat' => 'none',
  'sweets' => 'chocolate');

and I call my function:

$simpleXmlContents =  $this->convertArray->assocToXml($myArray, 'diner');

I would get the following in myfile.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  • how to add xml tag attribute in array. example:- <Events count="2">er</Events> how to add count="2" . Commented Feb 21, 2023 at 6:51
  • that is a good question @jigarPatel I have no answer to that yet and I think you may need to do a plugin solution for that because Magento does not seem to provide an out-of-the-box solution.
    – Isolde
    Commented Mar 10, 2023 at 13:55

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.