Reading list Switch to dark mode

    Read Data from XSD file in Magento 2

    Updated 23 May 2023

    In this blog, we are going to learn how we can read data from an XSD file.

    XSD (XML Schema Definition), a recommendation of the World Wide Web Consortium (W3C), specifies how to formally describe the elements in an Extensible Markup Language (XML) document. Programmers use it to verify each piece of item content in a document, to assure it adheres to the description of the element it is placed in.

    In Magento 2, it provides \Magento\Framework\Filesystem\DriverInterface interface, where the fileGetContents method is declared, we use this method to read/get file content or data from any relative or absolute path or an URL. When we will read the XML file using an URL we can convert it into an Array. We will use these steps in our Block file ReadXsd.php inside app/code/Vendor/CustomModule/Block/ directory.

    Please follow the below steps to get the result from an XSD file:
    Note: To know how to Create an extension in Magento 2, you can click here
    Additionally, To know more about magento modules, you can click here

    1. create page layout file

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    File Path: app/code/Webkul/CustomModule/view/frontend/layout/custommodule_test_index.xml

    <?xml version="1.0"?>
    <page xmlns:xsi="" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
            <referenceContainer name="content">
                <block class="Webkul\CustomModule\Block\ReadXsd" name="custom_module" template="Webkul_CustomModule::readXsd.phtml"/>

    2. Now, create phtml file.

    File Path: app/code/Webkul/CustomModule/view/frontend/templates/readXsd.phtml

    $filename = "RawMaterials";
    $xsdData = $block->getXsdFileContent($filename);

    3. Now, create block file ReadXsd.php

     * Webkul Software.
     * @category  Webkul
     * @package   Webkul_CustomModule
     * @author    Webkul Software Private Limited
     * @copyright Webkul Software Private Limited (
     * @license
    namespace Webkul\CustomModule\Block;
    use Magento\Framework\View\Element\Template;
    class ReadXsd extends Template
        public const XSD_PATH = '/view/base/web/files';
         * @var \Magento\Framework\Filesystem\Driver\File
        private $fileSystemDriver;
         * @var \Magento\Framework\Json\Helper\Data
        private $jsonHelper;
         * @var \Magento\Framework\Module\Dir\Reader
        private $reader;
         * @var \Magento\Framework\Module\Dir\Reader
        private $modulePath;
         * Constructor
         * @param \Magento\Framework\Filesystem\Driver\File $fileSystemDriver
         * @param \Magento\Framework\Json\Helper\Data $jsonHelper
         * @param \Magento\Framework\Module\Dir\Reader $reader
         * @param \Magento\Framework\View\Element\Template\Context $context
        public function __construct(
            \Magento\Framework\Filesystem\Driver\File $fileSystemDriver,
            \Magento\Framework\Json\Helper\Data $jsonHelper,
            \Magento\Framework\Module\Dir\Reader $reader,
            \Magento\Framework\View\Element\Template\Context $context
        ) {
            $this->fileSystemDriver = $fileSystemDriver;
            $this->jsonHelper = $jsonHelper;
            $this->reader = $reader;
         * Function getXsdFileContent
         * @param string $fileName
         * @return array
        public function getXsdFileContent($fileName)
            try {
                $data = false;
                $mageDir = $this->getXsdFileFullPath($fileName);
                if ($this->fileSystemDriver->isExists($mageDir)) {
                    $doc = new \DOMDocument();
                    $doc->preserveWhiteSpace = false;
                    $tempFileDir = BP .  '/pub/media/' . 'temp.xml';
                    $xmlfile = $this->fileSystemDriver->fileGetContents($tempFileDir);
                    $parseObj = str_replace($doc->lastChild->prefix.':', "", $xmlfile);
                    $parseObj = str_replace('wm'.':', "", $parseObj);
                    $ob = simplexml_load_string($parseObj);
                    $json  = $this->jsonHelper->jsonEncode($ob);
                    $data = $this->jsonHelper->jsonDecode($json);
                return $data;
            } catch (\Exception $e) {
                throw $e;
         * Function getXsdFileFullPath
         * @param string $fileName
         * @return string
        public function getXsdFileFullPath($fileName)
            $modulePath = $this->getModulePath();
            $mageDir = $modulePath.self::XSD_PATH."/".$fileName.".xsd";
            return $mageDir;
         * Function getModulePath
         * @return mixed
        public function getModulePath()
            if (!$this->modulePath) {
                $this->modulePath = $this->reader->getModuleDir('', 'Webkul_CustomModule');
            return $this->modulePath;

    4. When we hit the Controller in the browser’s window, we get the result of XSD file like below:

    screenshot image

    Also, you can check our other blog to know how to read data from XML file in magento 2 by clicking here

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*

    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home