steave
answered Apr 28 '23 00:00
Here is a sample PHP script that can help you convert sitemap.xml to a well-formatted format:
<?php
$xml = simplexml_load_file("sitemap.xml");
$newXml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset></urlset>');
foreach ($xml->url as $url) {
$newUrl = $newXml->addChild('url');
foreach ($url->children() as $child) {
$newUrl->addChild($child->getName(), (string) $child);
}
}
$newXml->asXML('sitemap-formatted.xml');
?>
This script loads the sitemap.xml file using the simplexml_load_file function and creates a new XML document using the SimpleXMLElement constructor. Then, it loops through each sitemap entry and copies each sub-node to the new XML document using addChild function.
Finally, the script saves the new XML document to sitemap-formatted.xml using the asXML function.
Note that this script assumes that sitemap.xml is in the same directory as the PHP file. You may need to modify the file path if your sitemap is stored in a different location.