1

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).

Community
  • 1
  • 1
Carmaox
  • 7
  • 3
  • 2
    Possible duplicate of [Parse XML with Namespace using SimpleXML](http://stackoverflow.com/questions/595946/parse-xml-with-namespace-using-simplexml) – har07 Feb 28 '16 at 11:16

1 Answers1

1

"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)."

I might have misunderstood the problem, but you can combine the two approaches if you like. Use xpath(), as mentioned in the link above, only for accessing prefixed elements :

//obtain the information URL->IMAGE
$image_extracted = $single_section->xpath('image:image/image:loc');

echo "IMG Extracted: " . $image_extracted[0] . "<br>";

Quick test : https://eval.in/526834

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137