[英]PHP DomDocument error - XML declaration not at the begginning of document
[英]Format XML Document created with PHP - DOMDocument
我試圖格式化我的 XML 文件在輸出時的外觀。 現在,如果你去這里查看源代碼,你會看到文件的樣子。
我創建文件的 PHP 是:(注意,$links_array 是一個 url 數組)
header('Content-Type: text/xml');
$sitemap = new DOMDocument;
// create root element
$root = $sitemap->createElement("urlset");
$sitemap->appendChild($root);
$root_attr = $sitemap->createAttribute('xmlns');
$root->appendChild($root_attr);
$root_attr_text = $sitemap->createTextNode('http://www.sitemaps.org/schemas/sitemap/0.9');
$root_attr->appendChild($root_attr_text);
foreach($links_array as $http_url){
// create child element
$url = $sitemap->createElement("url");
$root->appendChild($url);
$loc = $sitemap->createElement("loc");
$lastmod = $sitemap->createElement("lastmod");
$changefreq = $sitemap->createElement("changefreq");
$url->appendChild($loc);
$url_text = $sitemap->createTextNode($http_url);
$loc->appendChild($url_text);
$url->appendChild($lastmod);
$lastmod_text = $sitemap->createTextNode(date("Y-m-d"));
$lastmod->appendChild($lastmod_text);
$url->appendChild($changefreq);
$changefreq_text = $sitemap->createTextNode("weekly");
$changefreq->appendChild($changefreq_text);
}
$file = "sitemap.xml";
$fh = fopen($file, 'w') or die("Can't open the sitemap file.");
fwrite($fh, $sitemap->saveXML());
fclose($fh);
}
通過查看源文件可以看出,該文件並不像我希望的那樣可讀。 有什么辦法可以讓我格式化節點嗎?
謝謝,
列維
檢查DOMDocument
的formatOutput設置。
$sitemap->formatOutput = true
不僅僅是 PHP,還有一個 XML 樣式表: XSLT ,它可以將 XML 格式化為看起來不錯的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.