The code below works fine except that it doesn't break lines or add tabs.
<?php
$xml = simplexml_load_file("sitemap-1.xml"); //This line will load the XML file.
$sxe = new SimpleXMLElement($xml->asXML()); //In this line it create a SimpleXMLElement object with the source of the XML file.
//The following lines will add a new child and others child inside the previous child created.
$person = $sxe->addChild("url");
$person->addChild("loc", "http://www.mysite.com/omg-new-url.html");
$person->addChild("lastmod", "2009-02-13");
//This next line will overwrite the original XML file with new data added
$sxe->asXML("sitemap-1.xml");
?>
//It comes out like this:
<url>
<loc>http://www.mysite.com/old-url.html</loc>
<lastmod>2009-02-13</lastmod>
</url><url><loc>http://www.mysite.com/omg-new-url.html</loc><lastmod>2009-02-13</lastmod></url>
//Instead of like this:
<url>
<loc>http://www.mysite.com/old-url.html</loc>
<lastmod>2009-02-13</lastmod>
</url>
<url>
<loc>http://www.mysite.com/omg-new-url.html</loc>
<lastmod>2009-02-13</lastmod>
</url>
Doing Either of these (or variations) does not work:
<?php
$xml = simplexml_load_file("sitemap-1.xml"); //This line will load the XML file.
$sxe = new SimpleXMLElement($xml->asXML()); //In this line it create a SimpleXMLElement object with the source of the XML file.
//The following lines will add a new child and others child inside the previous child created.
$person = $sxe->addChild("url")."\n";
$person->addChild("loc", "http://www.mysite.com/omg-new-url.html")."\n";
$person->addChild("lastmod", "2009-02-13")."\n";
//This next line will overwrite the original XML file with new data added
$sxe->asXML("sitemap-1.xml");
?>
<?php
$xml = simplexml_load_file("sitemap-1.xml"); //This line will load the XML file.
$sxe = new SimpleXMLElement($xml->asXML()); //In this line it create a SimpleXMLElement object with the source of the XML file.
//The following lines will add a new child and others child inside the previous child created.
$person = $sxe->addChild("url\n");
$person->addChild("loc", "http://www.mysite.com/omg-new-url.html\n");
$person->addChild("lastmod", "2009-02-13\n");
//This next line will overwrite the original XML file with new data added
$sxe->asXML("sitemap-1.xml");
?>