格式化PHP simpleXML文件

格式化PHP simpleXML文件,php,xml,simplexml,domdocument,sitemap,Php,Xml,Simplexml,Domdocument,Sitemap,我正在创建简单的PHP代码来生成sitemap.xml文件,但问题是,该文件未格式化。如果文件较小,我可以在浏览器中查看,但它大约有1.5MB,Chrome在加载时就放弃了IE加载,您可以查看它,但它确实很难 我甚至在这个网站上搜索并尝试了不同的解决方案,但没有一个有效,所以我谦虚地请求你的帮助。另外,我使用最新的PHP版本,我的服务器运行在基于Unix的操作系统上,如果这很重要的话 下面是Im使用的代码表中有数千行15.000+行,因此XML文件相当长: $xml = '<urlset

我正在创建简单的PHP代码来生成sitemap.xml文件,但问题是,该文件未格式化。如果文件较小,我可以在浏览器中查看,但它大约有1.5MB,Chrome在加载时就放弃了IE加载,您可以查看它,但它确实很难

我甚至在这个网站上搜索并尝试了不同的解决方案,但没有一个有效,所以我谦虚地请求你的帮助。另外,我使用最新的PHP版本,我的服务器运行在基于Unix的操作系统上,如果这很重要的话

下面是Im使用的代码表中有数千行15.000+行,因此XML文件相当长:

$xml = '<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">';

$xml .= "<url>";
$xml .= "<loc>MY.URL.COM/</loc>";
$xml .= "<changefreq>always</changefreq>";
$xml .= "<priority>1.00</priority>";
$xml .= "</url>";

$xml .= "<url>";
$xml .= "<loc>MY.URL.COM/upload.php</loc>";
$xml .= "<changefreq>weekly</changefreq>";
$xml .= "<priority>0.80</priority>";
$xml .= "</url>";

$select = dbquery("SELECT * FROM MY TABLE");
while ($data = dbarray($select)) {
    $xml .= "<url>";
    $xml .= "<loc>MY.URL.COM/?id=".$data['id']."</loc>";
    $xml .= "<changefreq>daily</changefreq>";
    $xml .= "<priority>0.50</priority>";
    $xml .= "</url>";
}

$xml .= '</urlset>';

$sxml = new SimpleXMLElement($xml);

$dom = new DOMDocument('1.0');
$dom->formatOutput = true;
$dom->loadXML($sxml->asXML());
$dom->saveXML("sitemap.xml");
我所说的不可读是指它保存在一行中,如下所示:

<?xml version="1.0"?>
<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><loc>MY.URL.COM?id=1</loc><changefreq>daily</changefreq><priority>0.50</priority></url><url><loc>MY.URL.COM?id=2</loc><changefreq>daily</changefreq><priority>0.50</priority></url><url><loc>MY.URL.COM?id=5</loc><changefreq>daily</changefreq><priority>0.50</priority></url><url><loc>MY.URL.COM?id=7</loc><changefreq>daily</changefreq><priority>0.50</priority></url></urlset>

另一个需要注意的问题是,如果您的代码运行的服务器是Unix/Linux,那么它将使用Unix的行尾LF,如果使用Windows编辑器或浏览器查看,则可能无法正确显示CR-LF。

编辑开始 //您不需要simplexml //$sxml=新的simplexmlement$xml; $dom=新的DOMDocument'1.0'; $dom->formatOutput=true; $dom->loadXML$xml;//可以使用xml字符串 $dom->savesitemap.xml;//需要使用save而不是saveXML 编辑结束 您第一次编写的代码将执行正确的格式设置。使用simplexml将新元素/节点添加到sitemap.xml时会出现问题

如果您想要一个格式正确的XML,那么每次都需要使用DOMDocument保存它,这与您最初所做的相同

你在干什么

尝试将其更改为:


希望有帮助。

未格式化是指它缺少可读的缩进和换行符?没错。我将编辑这个问题。您使用的$dom->formatOutput=true方法是处理这个问题的典型方法。它是做什么的,而不是你期望它做什么的,也就是说,它是如何不工作的?另外,请在将字符串$xml加载到SimpleXML之前发布一个更完整的示例。添加了你要求的内容。基本上,虽然通过整个数据库,其中约有15000多个结果。第二个代码只是附加到已经存在的XML文件中,所以我不必总是遍历整个数据库。好吧,在浏览器中显示ok,我在开发版本上测试代码,开发版本在数据库中没有那么多记录,所以它实际上是加载的。有没有办法伪造它?因为我的服务器是基于Unix的。不确定伪造它是什么意思,但如果您想检查文件是否有cr lf或lf,则只有一个简单的if str_pos\l\n,$xml{print Windows EOL}应该显示给您。不确定伪造它是什么意思,但如果您想检查文件是否有cr lf或lf,则只有一个简单的if str_pos\r\n,$xml{print Windows EOL}应该显示给您。您还可以尝试通过unix2dos运行保存的文件,看看您得到了什么。我还编写了Unix utils EOL工具的PHP版本,可以在GitHub上找到。它们是从CLI运行的,需要安装composer,但如果需要,可以直接使用主lib/Converter类。我自己也有这个需要,所以决定试着写下来。剪掉我上面的编辑。谢谢你提供的信息,不管怎样它现在已经修复了。我试图在创建文件时手动添加\r\n,但函数saveXML只是删除了这些内容。您好,第一个代码没有执行propper格式化。我在dev页面上尝试它,没有代码的第二部分,xml文件是未格式化的:好的,现在它甚至可以与load$sxe一起工作。。。。。问题在于保存功能。我也会尝试附加它。好的,试过了,效果不错。我将把工作代码添加到问题中。
<?xml version="1.0"?>
<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><loc>MY.URL.COM?id=1</loc><changefreq>daily</changefreq><priority>0.50</priority></url><url><loc>MY.URL.COM?id=2</loc><changefreq>daily</changefreq><priority>0.50</priority></url><url><loc>MY.URL.COM?id=5</loc><changefreq>daily</changefreq><priority>0.50</priority></url><url><loc>MY.URL.COM?id=7</loc><changefreq>daily</changefreq><priority>0.50</priority></url></urlset>
<?xml version="1.0"?>
<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>
        <loc>MY.URL.COM?id=1</loc>
        <changefreq>daily</changefreq>
        <priority>0.50</priority>
    </url>
    <url>
        <loc>MY.URL.COM?id=2</loc>
        <changefreq>daily</changefreq>
        <priority>0.50</priority>
    </url>
    <url>
        <loc>MY.URL.COM?id=5</loc>
        <changefreq>daily</changefreq>
        <priority>0.50</priority>
    </url>
    <url>
        <loc>MY.URL.COM?id=7</loc>
        <changefreq>daily</changefreq>
        <priority>0.50</priority>
    </url>
</urlset>
$xml = "<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'>";

$select = dbquery("SELECT * FROM MY TABLE");
while ($data = dbarray($select)) {
    $xml .= "<url>";
    $xml .= "<loc>MY.URL.COM/?id=".$data['id']."</loc>";
    $xml .= "<changefreq>daily</changefreq>";
    $xml .= "<priority>0.50</priority>";
}

$xml .= '</urlset>';
$sitemap = simplexml_load_file($xml);
$sxe = new SimpleXMLElement($xml);

$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml);
$dom->save("sitemap.xml");
$sitemap = simplexml_load_file("sitemap.xml");
$url = $sitemap->addChild('url');
$url->addChild("loc", "MY.URL.COM/?id=".$get_id['id']);
$url->addChild("changefreq", "daily");
$url->addChild("priority", "0.50");

$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($sitemap->asXML());
$dom->save('sitemap.xml');
$sitemap = simplexml_load_file("sitemap.xml");
$url = $sitemap->addChild('url');
$url->addChild("loc", "MY.URL.COM/?id=".$get_id['id']);
$url->addChild("changefreq", "daily");
$url->addChild("priority", "0.50");
$sitemap->asXML("sitemap.xml");
$sitemap = simplexml_load_file("sitemap.xml");
$url = $sitemap->addChild('url');
$url->addChild("loc", "MY.URL.COM/?id=".$get_id['id']);
$url->addChild("changefreq", "daily");
$url->addChild("priority", "0.50");

$dom = new DOMDocument('1.0',LIBXML_NOBLANKS);
$dom->formatOutput = true;
$dom->loadXML($sitemap->asXML());
$dom->saveXML('sitemap.xml');