PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Jun 22, 2018 6:05 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Tue Mar 06, 2018 2:53 pm 
Offline
Forum Commoner

Joined: Mon Sep 27, 2004 10:56 am
Posts: 68
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:
  /**
     * 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();
    }
}




Top
 Profile  
 
PostPosted: Wed Mar 07, 2018 7:46 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13564
Location: New York, NY, US
Instead of this:
Syntax: [ Download ] [ Hide ]
            $xml = simplexml_load_file($file, "SimpleXMLElement", LIBXML_NOCDATA);

Do this:
Syntax: [ Download ] [ Hide ]
$json = file_get_contents($file);
$data = json_decode($json);

Also, why are you JSON encoding XML and then immediately decoding it?
Syntax: [ Download ] [ Hide ]
            $json = json_encode($xml);
            $data = json_decode($json, TRUE);

_________________
(#10850)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group