更新时间:2023-11-18 21:59:16
您将在每个循环中覆盖XML.考虑以root开头文档,并在foreach
循环之外关闭文档:
You are overwriting your XML with each loop. Consider starting document with root and closing document outside the foreach
loop:
$writer->openURI('./cache/xmls/posts-'.$news['MONTH'].'-'.$news['YEAR'].'.xml');
$writer->startDocument('1.0','UTF-8');
$writer->setIndent(4);
$writer->startElement('urlset');
foreach ($newsdata as $news){
$writer->startElement('url');
$writer->writeElement('loc',$news['title']);
$writer->endElement();
}
$writer->endElement();
$writer->endDocument();
$writer->flush();