I get this error:
Call to a member function addChild() on a non-object
But the XML file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<urlset>
<url>
<loc>http://mysite.com/</loc>
<changefreq>weekly</changefreq>
<priority>1.00</priority>
</url>
</urlset>
And I'm doing this:
//APPEND TO SITEMAP
$file = '../sitemap.xml';
$xml = simplexml_load_file($file);
$urlset = $xml->urlset;
$urls = $urlset->addChild('url');
$urls->addAttribute("mongoID", $theAuthorUniqueMongoID);
$urls->addChild('loc', 'http://mysite.com/author/'.$authorLink.'/');
$urls->addChild('changefreq', 'monthly');
$urls->addChild('priority', '0.80');
$xml->asXML($file);
I'm basically just appending some stuff to my site map. I was never any good at XML but I'm not sure what I'm doing wrong on this one.
You should change this:
$urlset = $xml->urlset;
$urls = $urlset->addChild('url');
Into:
$urls = $xml->addChild("url");
It will add contents to the root node of the XML you loaded. Thus running the script once yields the following output:
<?xml version="1.0" encoding="UTF-8"?>
<urlset>
<url>
<loc>http://mysite.com/</loc>
<changefreq>weekly</changefreq>
<priority>1.00</priority>
</url>
<url mongoID="">
<loc>http://mysite.com/author//</loc>
<changefreq>monthly</changefreq>
<priority>0.80</priority>
</url>
</urlset>
I changed the formatting of the output so it was easier to read. The addition is actually just a single line.