[英]PHP DOMDocument Writing Sitemap Problems - Uncaught exception 'DOMException'
I keep getting the following error when I add xmlns to my xml written with DOMDocument 将xmlns添加到用DOMDocument编写的xml中时,我不断收到以下错误
Fatal error: Uncaught exception 'DOMException' with message 'Invalid Character Error' in... 致命错误:...中出现未捕获的异常“ DOMException”,消息为“无效字符错误”
$xml = new DOMDocument("1.0", "UTF-8");
$xml_urlset = $xml->createElement('urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"');
$xml_url = $xml->createElement("url","this text");
$xml_urlset->appendChild($xml_url);
$xml->appendChild($xml_urlset);
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->save("test.xml");
Also, even though I have formatOutput = true I still get everything written out as one long line: 另外,即使我的formatOutput = true,我仍然将所有内容写成一行:
<urlset>xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"<url>this text</url></urlset>
I was trying to set things so this could be outputed for the urlset 我正在尝试设置东西,以便可以为urlset输出
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
Thank you for any help you can give me. 谢谢您能给我的任何帮助。
Your call to createElement
is completely bogus. 您对
createElement
调用完全是伪造的。 You can't add attributes that way. 您不能以这种方式添加属性。 Try this:
尝试这个:
<?php
$xml = new DOMDocument("1.0", "UTF-8");
$xml_urlset = $xml->createElement('urlset');
$xml_urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$xml_urlset->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$xml_urlset->setAttribute('xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');
$xml_url = $xml->createElement("url","this text");
$xml_urlset->appendChild($xml_url);
$xml->appendChild($xml_urlset);
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->save("test.xml");
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.