简体   繁体   English

格式化PHP simpleXML文件

[英]Format PHP simpleXML file

Im creating simple PHP code to generate sitemap.xml file, but the problem is, that the file is unformatted. 我创建了简单的PHP代码来生成sitemap.xml文件,但问题是该文件未格式化。 If the file was smaller, I could look at it in browser, but it has about 1,5 MB and Chrome just gives up while loading it (IE loads it and you can view it, but its lagging really hard). 如果文件较小,我可以在浏览器中查看它,但它只有约1.5 MB,Chrome只是在加载文件时放弃了(IE加载了文件,您可以查看它,但它的确滞后)。

I googled and tried different solutions (even from this site), but none of them worked, so Im humbly asking for your help. 我用谷歌搜索并尝试了不同的解决方案(即使从该站点也是如此),但是它们都不起作用,所以林谦虚地向您寻求帮助。 Also Im using the newest PHP version and my server runs on Unix based OS if thats important. 我也使用最新的PHP版本,如果这很重要,则我的服务器可以在基于Unix的操作系统上运行。

Here is code Im using (there are couple thousands (15.000+) rows in the table, so XML file is pretty long: 这是代码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");

Also, every time after new page is added Im running this code to append it to sitemap file and its appending it into one row, so need to fix that too. 另外,每次添加新页面后,我都会运行此代码以将其附加到站点地图文件并将其附加到一行,因此也需要修复该问题。 Im trying both codes separately, so Im sure the first one doesnt format the document at all. 我要分别尝试这两个代码,因此请确保第一个代码完全不格式化文档。

$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");

By Unreadable I mean its saved in one line like this: 不可读是指将其保存在这样的一行中:

<?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>

Instead of: 代替:

<?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>

** **

WORKING CODE: 工作代码:

** **

$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");

Code to append to existing xml file: 附加到现有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');

要查看的另一件事是,如果运行代码的服务器是Unix / Linux,它将使用Unix行尾(LF),如果在Windows编辑器或浏览器中查看,则可能无法正确显示(CR-LF)。

Edit Start 编辑开始

//you don't need simplexml
// $sxml = new SimpleXMLElement($xml);

$dom = new DOMDocument('1.0');
$dom->formatOutput = true;
$dom->loadXML($xml); // can use the xml string
$dom->save("sitemap.xml"); // need to use save() rather than saveXML

Edit End 编辑结束

The code that you have written for the first time will do a proper formatting. 您第一次编写的代码将进行正确的格式化。 Problem will arise when you add a new element/node to sitemap.xml using simplexml. 使用simplexml将新元素/节点添加到sitemap.xml时,将出现问题。

If you want a properly formatted XML, you will need to save it using DOMDocument every time, the same one that you are doing initially. 如果要使用格式正确的XML,则每次都需要使用DOMDocument进行保存,与开始时相同。

What you are doing 你在做什么

$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");

Try changing it to this: 尝试将其更改为此:

$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');

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM