开发者

Sitemap creation with DOMDocument throws parsing error

开发者 https://www.devze.com 2023-03-13 16:51 出处:网络
I\'m creating a sitemap in XML, it works well with one record displayed, but when including 1+ records, it throws an error:

I'm creating a sitemap in XML, it works well with one record displayed, but when including 1+ records, it throws an error:

XML Parsing Error: junk after document element

Which shows this code here:

<?xml version="1.0" encoding="UTF-8"?>
<url><loc>http://www.mywebsite.com/page/1</loc><changefreq>daily</changefreq><priority>0.6</priority></url>
<url><loc>http://www.mywebsite.com/page/2</loc><changefreq>daily</changefreq><priority>0.6</priority></url>

My code:

$xml = new 开发者_C百科DOMDocument('1.0', 'UTF-8');

for($i = 0; $i < 2; $i++)
{
    $url = $xml->createElement('url');
    $xml->appendChild($url);

    $website_url = 'http://www.mywebsite.com/page/' . $i;

    $loc = $xml->createElement('loc', $website_url);
    $url->appendChild($loc);

    $change = $xml->createElement('changefreq', 'daily');
    $url->appendChild($change);

    $priority = $xml->createElement('priority', '0.6');
    $url->appendChild($priority);
}

header('Content-type: text/xml');
echo $xml->saveXML();

Why is it throwing this kind of error when the XML seems valid to me?


At least in your example, you have two root nodes (<url>), as this is not allowed in xml, the second is the junk after document element.

You're missing the <urlset> root node, see: http://www.sitemaps.org/protocol.php

0

精彩评论

暂无评论...
验证码 换一张
取 消