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
File Path: app/code/Webkul/CustomModule/view/frontend/layout/custommodule_test_index.xml
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="content"> <block class="Webkul\CustomModule\Block\ReadXsd" name="custom_module" template="Webkul_CustomModule::readXsd.phtml"/> </referenceContainer> </body> </page>
2. Now, create phtml file.
File Path: app/code/Webkul/CustomModule/view/frontend/templates/readXsd.phtml
<?php $filename = "RawMaterials"; $xsdData = $block->getXsdFileContent($filename); ?> <pre> <?php print_r($xsdData); ?> </pre>
3. Now, create block file ReadXsd.php
<?php /** * Webkul Software. * * @category Webkul * @package Webkul_CustomModule * @author Webkul Software Private Limited * @copyright Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ 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; parent::__construct($context); } /** * 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; $doc->load($mageDir); $tempFileDir = BP . '/pub/media/' . 'temp.xml'; $doc->save($tempFileDir); $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:

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