I have the following XML schema:
<url>
<loc>
https://www.domain.com/artcile-title-x
</loc>
<mobile:mobile/>
<image:image>
<image:loc>
https://www.domain.com/images/file_name.jpg
</image:loc>
<image:title>file_name.jpg</image:title>
</image:image>
<lastmod>2015-11-29T06:17:25+00:00</lastmod>
<changefreq>monthly</changefreq>
</url>
Using the simplexml_load_file functions I can correctly extract "loc". The problem is with "image". I tried many tests, but nothing. Maybe simplexml_load_file is not the correct one?
For instance:
//XML url location
$xmlurl = "https://www.domain.com/sitemap.xml";
//get the content in the $xmlcode var
$xmlcode = simplexml_load_file($xmlurl);
//for each "section" extracted obtain LOC and IMAGE
foreach($xmlcode->url as $single_section)
{
//obtain the information URL->LOC
$loc_extracted = $single_section->loc;
echo "URL Extracted: " . $loc_extracted . "<br>";
//obtain the information URL->IMAGE
$image_extracted = $single_section->image->loc;
echo "IMG Extracted: " . $image_extracted[0] . "<br>";
}
I have tried many tests like:
$image_extracted = $single_section->image->image->loc;
or
$image_extracted = $single_section->image->image->image->loc;
or
$image_extracted = $single_section->image; //and use it as an array
or
$image_extracted = $single_section->image['image']->loc;
or
$image_extracted = $single_section->image['image']->image->loc;
It is important to observe the XML file and how it has been built. From the same "url" section I have to extract both "loc" and "images:images/images:loc" detail.
I tried with this possible solution: Parse XML with Namespace using SimpleXML
but it doesn't fit with the need to extract a simple parameter (loc) and a parameter with nodes (images/loc).