0

How to retrieve image url from below xml file? I am able to retrieve url, loc lastmod, changefreq, priority in array or object format. But image, loc, title is not. Please tell me Which kind of solution exist?

I am able to retrieve below xml object.

    SimpleXMLElement Object
(
    [url] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [loc] => https://test_url//search?tags=Auto%20Repairs
                    [lastmod] => 2017-08-15
                    [changefreq] => daily
                    [priority] => 0.5
                )
        )
)

My XML file code is like below.

<?xml version="1.0" encoding="utf-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    <url>
    <loc>https://test_url/search?tags=Auto%20Repairs</loc>
    <lastmod>2017-08-15</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
    <image:image>
    <image:loc>https://media.test_url/1476130698_business_images.png</image:loc>
    <image:title>RV Services Near Me</image:title>
    </image:image>
    <image:image>
    <image:loc>https://media.test_url/1470780022.jpg</image:loc>
    <image:title>RV Services Near Me</image:title>
    </image:image>
    <image:image>
    <image:loc>https://media.test_url/1477691994.jpg</image:loc>
    <image:title>RV Services Near Me</image:title>
    </image:image>
    <image:image>
    <image:loc>https://media.test_url/1466467993.jpg</image:loc>
    <image:title>RV Services Near Me</image:title>
    </image:image>
    </url>
    </urlset>
Eddie
  • 26,593
  • 6
  • 36
  • 58
Apple Dev
  • 35
  • 9

2 Answers2

1

Your dealing with elements in a specific namespace, but as they are all conveniently under a single namespace you can use the ability to fetch all the children of a specific node using this namespace (and the prefix in this case). So ->children("image", true); will extract all of the nodes in the image namespace and then use foreach() to go through each one at a time.

$xml = simplexml_load_file($fileName);
$images = $xml->url->children("image", true);
foreach ( $images as $image )   {
    echo $image->loc."=".$image->title.PHP_EOL;
}

This generates...

https://media.test_url/1470780022.jpg=RV Services Near Me
https://media.test_url/1477691994.jpg=RV Services Near Me
https://media.test_url/1466467993.jpg=RV Services Near Me
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Actually. Content is loading from file (sitemap.xml). I used `$xml= simplexml_load_file("mrv_sitemap.xml");` function. – Apple Dev Mar 30 '18 at 13:16
  • If I used above code in one string variable then it works But why it is not working with file. – Apple Dev Mar 30 '18 at 13:18
  • I've changed it to `simplexml_load_file()`, but you should be able to change $xml to be the data you've already loaded with the same data. – Nigel Ren Mar 30 '18 at 13:18
  • One thing is don't rely on an output of something like `print_r()` on a SimpleXMLElement - use `->asXML()` to show you the full content. – Nigel Ren Mar 30 '18 at 13:20
  • Yes. It works. But gives warning parse error after some iterations. `Warning: simplexml_load_string(): Entity: line 11936: parser error : Start tag expected, '<' not found in D:\xampp\htdocs\mrving_xml\index_image.php on line 56` – Apple Dev Mar 30 '18 at 13:27
  • Working file using SimpleXMLElement and ->asXML(); – Apple Dev Mar 30 '18 at 13:34
0

Please check below work around code. This will definitely help you to go ahead. Please let me know if this helps you.

<?php


$xmlstr = '<?xml version="1.0" encoding="utf-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    <url>
    <loc>https://test_url/search?tags=Auto%20Repairs</loc>
    <lastmod>2017-08-15</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
    <image:image>
    <image:loc>https://media.test_url/1476130698_business_images.png</image:loc>
    <image:title>RV Services Near Me</image:title>
    </image:image>
    <image:image>
    <image:loc>https://media.test_url/1470780022.jpg</image:loc>
    <image:title>RV Services Near Me</image:title>
    </image:image>
    <image:image>
    <image:loc>https://media.test_url/1477691994.jpg</image:loc>
    <image:title>RV Services Near Me</image:title>
    </image:image>
    <image:image>
    <image:loc>https://media.test_url/1466467993.jpg</image:loc>
    <image:title>RV Services Near Me</image:title>
    </image:image>
    </url>
    </urlset>';
    /* here i am deleting colon */
    $CleanXML = str_replace(["image:image", "image:loc", "image:title", "xmlns:image", "xsi:schemaLocation", "xmlns:xsi"],["imageimage","imageloc","imagetitle", "xmlnsimage", "xsischemaLocation", "xmlnsxsi"], $xmlstr);
    $ParseData = @simplexml_load_string($CleanXML);
    echo "<pre>";
    print_r($ParseData);