Read json file instead of XML, please help make the code

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
danjapro
New php-forum User
New php-forum User
Posts: 32
Joined: Wed Mar 02, 2011 9:30 am

Tue Mar 06, 2018 11:54 am

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.

Code: Select all

 /**
     * 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();
    }
}

Last edited by danjapro on Wed Mar 07, 2018 1:07 pm, edited 1 time in total.


Post Reply