danjapro Posted March 6, 2018 Share Posted March 6, 2018 I need some Help, I need to re-write my code slightly to read JSON files with Key:Value that there for VML. I just need to read json files instead of reading XML files. Can someone assist, I think it i simple adjustment, I am having hard time. /** * Function install * @param ModuleDataSetupInterface $setup * @param ModuleContextInterface $context * @return void */ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); $reflector = new \ReflectionClass('Incomm\Cms\Setup\UpgradeData'); $reflector->getFileName(); $contentDirectory = dirname($reflector->getFilename()) . "/../Content/"; $cmsBlocksFiles = glob("{$contentDirectory}Blocks/*xml"); $cmsPagesFiles = glob("{$contentDirectory}Pages/*xml"); $homePageFiles = glob("{$contentDirectory}Homepage/*xml"); /* CMS Blocks */ foreach ($cmsBlocksFiles as $file) { $xml = simplexml_load_file($file, "SimpleXMLElement", LIBXML_NOCDATA); $json = json_encode($xml); $data = json_decode($json, TRUE); unset($data["version"]); $tmpBlock = $this->blockFactory->create(['data' => $data]); $this->blockRepository->save($tmpBlock); } /* CMS Pages */ foreach ($cmsPagesFiles as $file) { $xml = simplexml_load_file($file, "SimpleXMLElement", LIBXML_NOCDATA); $json = json_encode($xml); $data = json_decode($json, TRUE); $page = $this->pageFactory->create(); $page->setTitle($data['title']) ->setIdentifier($data['identifier']) ->setIsActive(true) ->setPageLayout($data['page_layout']) ->setStores(array(0)) ->setContent($data['content']) ->save(); } /* Home page */ $newPage = $this->pageFactory->create()->load( 'home', 'identifier' ); if ($newPage->getId()) { foreach ($homePageFiles as $file) { $xml = simplexml_load_file($file, "SimpleXMLElement", LIBXML_NOCDATA); $json = json_encode($xml); $data = json_decode($json, TRUE); $newPage->setContent($data["content"]); $newPage->save(); } } $setup->endSetup(); } } Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted March 6, 2018 Solution Share Posted March 6, 2018 1. Fix those three lines near the top so they find the right files. 2. Replace the three stupid pairs of simplexml_load_file/json_encode lines with one that uses file_get_contents to load the JSON data into $json. Keep the json_decode lines. 3. There is no third step. That's assuming the JSON structure mirrors the XML structure. If it does not then you have to adjust the code accordingly. Quote Link to comment Share on other sites More sharing options...
danjapro Posted March 7, 2018 Author Share Posted March 7, 2018 That worked.. I had to make few addiontal changes for params, but otherwise worked!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.