Jump to content

Read JSON instead of XML, need some HELP


danjapro

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.