Skip to content

xml to extract data in php

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

                    )