简单 XML 调用布尔值的成员函数 addChild()

Simple XML Call to a member function addChild() on boolean

我在使用简单xmlPHP函数制作XML站点地图时遇到问题,情况几乎相同sitemap 标签时,它不起作用:

$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 to a member function addChild() on boolean

这是工作可靠:

$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");

您的问题是由简单的错误引起的。

(您 - 可能忘记更改 - 从 url 设置为 站点地图索引 的结束标记):

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

正确:

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