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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

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

Post by danjapro »

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


User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post by Christopher »

Instead of this:

Code: Select all

            $xml = simplexml_load_file($file, "SimpleXMLElement", LIBXML_NOCDATA);
Do this:

Code: Select all

$json = file_get_contents($file);
$data = json_decode($json);
Also, why are you JSON encoding XML and then immediately decoding it?

Code: Select all

            $json = json_encode($xml);
            $data = json_decode($json, TRUE);
(#10850)
Post Reply