dongye9991 2011-06-15 16:01
浏览 65
已采纳

使用DOMDocument创建站点地图会引发解析错误

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

  • 写回答

1条回答 默认 最新

  • dongzhong7299 2011-06-15 16:08
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?