Simple XML call to addChild () member function in boolean

I had a problem with the layout of the XML sitemap using a simple PHP xml function that has an almost equal situation, and the tag sitemapdoes not work:

$xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>');

$sitemap = $xml->addChild("sitemap");
$sitemap->addChild("loc", "http://www.example.com/sitemap-1.xml");

Fatal error: call member function addChild () in boolean

enter image description here

This works reliably :

$xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>');

$url = $xml->addChild("url");
$url->addChild("loc", "http://www.example.com/sitemap-2.xml");
+4
source share
1 answer

Your problem is caused by a simple error.

(you have - perhaps forgot to change - the closing tag from urlset to sitemapindex ):

$xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>');

Correctly:

$xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></sitemapindex>');
+2
source

Source: https://habr.com/ru/post/1653877/


All Articles