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 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统