Warning: Cannot use a scalar value as an array in /home/admin/public_html/forum/include/fm.class.php on line 757

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/include/fm.class.php on line 770
Форумы портала PHP.SU :: Версия для печати :: генерация xml при помощи php
Форумы портала PHP.SU » » XML и его обработка » генерация xml при помощи php

Страниц (1): [1]
 

1. s3ga - 01 Октября, 2014 - 10:00:23 - перейти к сообщению
Всем привет, для поисковиков пытаюсь сгенерировать xml при помощи php.
файл xml.php
CODE (htmlphp):
скопировать код в буфер обмена
  1. <?php
  2. header('Content-type: text/xml; charset=utf-8');
  3. echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  4. include('db.php');
  5. $xml='<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">';
  6. $xml.="<url>
  7. <loc>http://www.test.com/</loc>
  8. <changefreq>daily</changefreq>
  9. <priority>0.8</priority>
  10. </url>";
  11. $sql_art = 'select * from articles where static = 0 order by title limit 100';
  12. $query_art = mysql_query($sql_art);
  13. while ($row_art = mysql_fetch_assoc($query_art))
  14. {
  15. $xml.="<url>
  16. <loc>http://www.test.com/".$row_art['title_url'].".html</loc>
  17. <lastmod>".$row_art['add_date']."</lastmod>
  18. <changefreq>weekly</changefreq>
  19. <priority>0.8</priority>
  20. </url>";
  21. }
  22. $xml.='<urlset>';
  23. echo $xml;
  24. ?>
  25.  

Выводит ошибку

This page contains the following errors:

error on line 1 at column 50: AttValue: " or ' expected
Below is a rendering of the page up to the first error.
Пробелов перед и после <?php нету
(Добавление)
Изменил на header('Content-type: application/xml'); все заработало
2. tato - 02 Октября, 2014 - 00:42:48 - перейти к сообщению
PHP:
скопировать код в буфер обмена
  1.  
  2. $xml = new SimpleXMLElement(
  3.             '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>'
  4. );
  5.  
  6. $urls = [...someUrls...];
  7.  
  8. foreach( $urls as $url )
  9. {
  10.     $node = $xml->addChild('url');
  11.     $node->addChild('loc', $url);
  12.     $node->addChild('lastmod', date(DateTime::ISO8601, $lastmod));
  13. }
  14.  
  15. $dom = new DOMDocument('1.0', 'UTF-8');
  16. $dom->preserveWhiteSpace = false;
  17. $dom->formatOutput = true;
  18. $dom->loadXML($xml->asXML());
  19.  
  20. file_put_contents( 'sitemap.xml', $dom->saveXml() );
  21.  

 

Powered by ExBB FM 1.0 RC1