格式化使用PHP创建的XML文档 - DomDocument

Lev*_*evi 10 php xml 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);
    }
Run Code Online (Sandbox Code Playgroud)

通过查看源代码可以看出,该文件不像我希望的那样可读.我有什么方法可以格式化节点吗?

谢谢,
Levi

Anu*_*rag 9

检查中的formatOutput设置DOMDocument.

$sitemap->formatOutput = true
Run Code Online (Sandbox Code Playgroud)