简体   繁体   English

PHP DOMDocument编写站点地图问题-未捕获的异常'DOMException'

[英]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.

相关问题 PHP XML DOM未捕获的异常“ DOMException”,消息为“错误的文档错误” - PHP XML DOM Uncaught exception 'DOMException' with message 'Wrong Document Error' php domdocument 异常 loadHTMLFile - php domdocument exception loadHTMLFile 未捕获的异常&#39;DOMException&#39;,消息&#39;Not Found Error&#39; - Uncaught exception 'DOMException' with message 'Not Found Error' 带有消息'Hierarchy Request Error'的未捕获异常'DOMException' - Uncaught exception 'DOMException' with message 'Hierarchy Request Error' PHP DOMdocument问题(字符,文件) - PHP DOMdocument problems (characters, file) PHP HTML DOMDocument getElementById 问题 - PHP HTML DOMDocument getElementById problems 在PHP中使用DOMDocument创建XML的问题 - Problems with creating XML with DOMDocument in PHP 如何使用 PHP 和 DOMDocument 生成包含页面本地化版本的 Google Sitemap - How to generate a Google Sitemap with localized versions of a page using PHP and DOMDocument php 致命错误:未捕获的 ValueError:DOMDocument::loadHTML() - php Fatal error: Uncaught ValueError: DOMDocument::loadHTML() PHP appendChild给人致命的错误-消息“层次结构请求错误”的未捕获异常“ DOMException”-如何在标记后添加html - PHP appendChild giving nice fatal error -Uncaught exception 'DOMException' with message 'Hierarchy Request Error - how can I add html AFTER a tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM