How to Parse Sitemap XML Content using PHP

Viewed 1740

I have site that is on wordpress. I want to make a PHP Script that will parse XML content from the URL and show me in the form of table.

I had written below PHP Script to parse the data

<?php
$xml = simplexml_load_string(file_get_contents($url));
foreach ($xml->url as $url)
{
    ?>
    Loc: <?php echo $url->loc; ?>
    <br />
    Last Modified: <?php echo $url->lastmod; ?>
    <br />
    Image Title: <?php echo $xml->children('image', true)->image->title; ?>
    <br />
    Image Location: <?php echo $xml->children('image', true)->image->loc; ?>
    <hr />
    <?php
}
?>

Problem The Above PHP Script is working fine to parse Loc & Last Modified But Not Working for Image Loc & Image Title. Sitemap URL

view-source:http://getintoblog.com/post-sitemap.xml

Kindly Help.

1 Answers

If you take a closer look at the structure of the XML document you can see that the <image> tags you are trying to parse are children of <url>, not <xml>.

Change $xml->children('image', true) to $url->children('image', true) in your code and you should be fine.

Related