I created xml file in my module etc folder.i want to read this xml file and store into variable as a string.

\Magento\Framework\Filesystem\Directory\Read $read

$XMLGetRequest=$this->read->readFile('here i need xml file path');
  • Do you know the name of xml file? – Arkadii Chyzhov Feb 9 '16 at 12:31
  • yes.etc/paymentschema.xml – Sivakumar K Feb 9 '16 at 12:33
  • so do you want to find a path to this one file? Or you want to find all files with this name? Could you clarify your question? – Arkadii Chyzhov Feb 9 '16 at 12:38
  • this one file only. – Sivakumar K Feb 9 '16 at 12:39

Try to use \Magento\Framework\Xml\Parser and \Magento\Framework\Module\Dir\Reader

Something like this:

 class YourClass 
         * @var \Magento\Framework\Module\Dir\Reader
        protected $moduleDirReader;

         * @var \Magento\Framework\Xml\Parser
        private $parser;


        public function getValue() 
            $filePath = $this->moduleDirReader->getModuleDir('etc', 'Your_Module') 
               . '/paymentschema.xml'
            $parsedArray = $this->parser->load($filePath)->xmlToArray();
            return $parsedArray['xmlNodeName'];

there is actually a way to create a new configuration object in magento2 that can parse your configuration and validate it through an xsd all automatically.

I know they mentioned it in a fundamentals course but I can't find any reference now.


I think this is it: http://devdocs.magento.com/guides/v2.0/config-guide/config/config-create.html

Under Create Configuration Types

  • Actually i dont need to validate and get particular node.simply i need to store into a variable as a string. – Sivakumar K Feb 9 '16 at 13:54
  • ok i get that, then there is the simple and the right way ;) – David Verholen Feb 9 '16 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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