Tags
Asked 3 years ago
30 Jun 2021
Views 269
Ozella

Ozella posted

edit xml file (site-map) with php !

edit xml file (site-map) with php !
sandip

sandip
answered Apr 28 '23 00:00

To edit an XML file using PHP, you can follow these steps:

Load the XML file into a SimpleXMLElement object using the simplexml_load_file () function. For example:



$xml = simplexml_load_file('sitemap.xml');

Use the SimpleXMLElement object to make changes to the XML data as required. For example, to change the loc element value of the first url element, you could do:



$xml->url[0]->loc = 'https://example.com/new-location';

Save the modified XML back to the file using the asXML () function. For example:



$xml->asXML('sitemap.xml');

Here is an example code snippet that demonstrates how to change the value of the loc element of the first url element in a sitemap.xml file:



// Load the XML file
$xml = simplexml_load_file('sitemap.xml');

// Make changes to the XML data
$xml->url[0]->loc = 'https://example.com/new-location';

// Save the modified XML back to the file
$xml->asXML('sitemap.xml');

Make sure that the PHP script that modifies the XML file has the necessary file permissions to read and write to the file.
Post Answer