Parsing an XML Sitemap in PHP
This post shows you how to parse an XML sitemap using PHP. Let’s take a look at the below code:
xml-sitemap-parsing.php
<?php
// sitemap url or sitemap file
$sitemap = 'https://bytenota.com/sitemap.xml';
// get sitemap content
$content = file_get_contents($sitemap);
// parse the sitemap content to object
$xml = simplexml_load_string($content);
// retrieve properties from the sitemap object
foreach ($xml->url as $urlElement) {
// get properties
$url = $urlElement->loc;
$lastmod = $urlElement->lastmod;
$changefreq = $urlElement->changefreq;
$priority = $urlElement->priority;
// print out the properties
echo 'url: '. $url . '<br>';
echo 'lastmod: '. $lastmod . '<br>';
echo 'changefreq: '. $changefreq . '<br>';
echo 'priority: '. $priority . '<br>';
echo '<br>---<br>';
}
We first use file_get_contents
function to get XML sitemap content and then use simplexml_load_string
function to parse the content to object.
Finally, we iterate over the $xml->url
array, retrieve necessary properties, and echo the property values in view.
Output:
url: https://bytenota.com/
lastmod:
changefreq: always
priority: 1.0
---
url: https://bytenota.com/aspnet-get-ie-browser-version-using-csharp/
lastmod: 2018-05-30T04:11:52Z
changefreq: daily
priority: 0.9
---
url: https://bytenota.com/javascript-save-objects-in-html5-session-storage/
lastmod: 2018-05-25T08:28:08Z
changefreq: daily
priority: 0.7
---
url: https://bytenota.com/javascript-save-objects-in-html5-local-storage/
lastmod: 2018-05-25T06:08:41Z
changefreq: daily
priority: 0.7
Nice script thx, but it doesn t work when u try a sitemap index like yoast (wordpress) does. How can i change the script to be able to parse a file like that?
if u parsing sitemap index, u change this script foreach ($xml->url as $urlElement) replace whit foreach ($xml->sitemap as $urlElement)
thanks
Works perfectly. Exactly what I was looking for to add to my app.