0

Her are my code. I can't able to get full data in array format. Only Url get this php code. Where am I wrong.

$sitemap = 'videomap-1.xml';

$context = stream_context_create(
    array(
        "http" => array(
            "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
        )
    )
);

$xml = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
echo "<pre>"; print_r($array);exit;

XML structure:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<url>
<loc>https://www.google.com</loc>
<video:video>
<video:thumbnail_loc>abc.jpg</video:thumbnail_loc>
<video:title>abc</video:title>
<video:description>Watch thousands of 4k videos..</video:description>
<video:player_loc autoplay="ap=1">test.mp4</video:player_loc>
<video:duration>470</video:duration>
<video:publication_date>2022-06-18T14:24:05+00:00</video:publication_date>
<video:family_friendly>no</video:family_friendly>
<video:category/>
<video:platform relationship="allow">web</video:platform>
<video:live>no</video:live>
</video:video>
</url>

But output:

Array
(
    [url] => Array
        (
            [0] => Array
                (
                    [loc] => https://www.google.com
                )

I need output:

Array
    (
        [url] => Array
            (
                [0] => Array
                    (
                        [loc] => https://www.google.com
[video:video] => array (
[video:thumbnail_loc] => abc.jpg

                    )

1 Answer 1

0

Unfortunately you cannot properly convert XML attributes or partial NS attributes with json_encode. Just for fun i wrote simple example how you can convert XML into some tricky PHP array with keep all data. Output format is little different from your expectation and my goal was to share example how to get data.

<?php
declare(strict_types=1);

namespace Acme\StackExchange\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class XmlParser extends AbstractHelper
{
    public function parseString(string $xml, array $nsList = []): array
    {
        $xmlElement = simplexml_load_string($xml);
        if ($xmlElement) {
            return $this->parse($xmlElement, $nsList);
        }

        return [];
    }

    public function parse(\SimpleXMLElement $xmlNode, array $nsList = [], bool $isRoot = true): array
    {
        $output = [];

        if (!$isRoot) {
            if (count($xmlNode->attributes()) > 0) {
                $output['$'] = [];
                foreach ($xmlNode->attributes() as $key => $value) {
                    $output['$'][$key] = (string)$value;
                }
            }

            $textValue = trim((string)$xmlNode);
            if (!empty($textValue)) {
                $output['_'] = $textValue;
            }

            $childrenList = [$xmlNode->children()];
            if ($nsList) {
                foreach ($nsList as $ns) {
                    $childrenList[] = $xmlNode->children($ns, true);
                }
            }

            foreach ($childrenList as $children) {
                foreach ($children as $childNode) {
                    $childName = $childNode->getName();
                    if (!array_key_exists($childName, $output)) {
                        $output[$childName] = [];
                    }
                    $output[$childName][] = $this->parse($childNode, $nsList, false);
                }
            }
        } else {
            $nodeName = $xmlNode->getName();
            $output[$nodeName] = [$this->parse($xmlNode, $nsList, false)];
        }

        return $output;
    }
}

How to use

$helper =\Magento\Framework\App\ObjectManager::getInstance()
            ->get(\Acme\StackExchange\Helper\XmlParser::class);
var_export($helper->parseString($xml, ['video']));

Example output:

array (
  'urlset' => 
  array (
    0 => 
    array (
      'url' => 
      array (
        0 => 
        array (
          'loc' => 
          array (
            0 => 
            array (
              '_' => 'https://www.google.com',
            ),
          ),
          'video' => 
          array (
            0 => 
            array (
              'thumbnail_loc' => 
              array (
                0 => 
                array (
                  '_' => 'abc.jpg',
                ),
              ),
              'title' => 
              array (
                0 => 
                array (
                  '_' => 'abc',
                ),
              ),
              'description' => 
              array (
                0 => 
                array (
                  '_' => 'Watch thousands of 4k videos..',
                ),
              ),
              'player_loc' => 
              array (
                0 => 
                array (
                  '$' => 
                  array (
                    'autoplay' => 'ap=1',
                  ),
                  '_' => 'test.mp4',
                ),
              ),
              'duration' => 
              array (
                0 => 
                array (
                  '_' => '470',
                ),
              ),
              'publication_date' => 
              array (
                0 => 
                array (
                  '_' => '2022-06-18T14:24:05+00:00',
                ),
              ),
              'family_friendly' => 
              array (
                0 => 
                array (
                  '_' => 'no',
                ),
              ),
              'category' => 
              array (
                0 => 
                array (
                ),
              ),
              'platform' => 
              array (
                0 => 
                array (
                  '$' => 
                  array (
                    'relationship' => 'allow',
                  ),
                  '_' => 'web',
                ),
              ),
              'live' => 
              array (
                0 => 
                array (
                  '_' => 'no',
                ),
              ),
            ),
          ),
        ),
      ),
    ),
  ),
)

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .