8

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?

2
  • 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
    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 Jul 10, 2017 at 9:21

2 Answers 2

1

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);
          fclose($myfile);
        } catch (Exception $e) {
          $this->_logger($e->getMessage());
        }
        return;
     }

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

1
  • 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
    Mar 21, 2018 at 13:07
0

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');
$this->file->write($myXmlFile,$simpleXmlContents->asXML());

I would get the following in myfile.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<diner><fruit>apple</fruit><vegetables><vegetable_1>carrot</vegetable_1> 
<vegetable_2>tomato</vegetable_2></vegetables><meat>none</meat> 
<sweets>chocolate</sweets></diner>
2
  • how to add xml tag attribute in array. example:- <Events count="2">er</Events> how to add count="2" . Feb 21 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
    Mar 10 at 13:55

Your Answer

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

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