问题
UPDATE To make it a little more clear to some about what's going on, my page does NOT display the document tree(the first time I've ever seen this). Just one long line of my xml content WITHOUT any xml elements/tags.
I followed this tutorial for creating a dynamic sitemap in cakephp. This tutorial was written in 2008 and I (have inherited) am working in a cakephp 1.3 app (I am assuming this tutorial would be valid for 1.3? I've looked at some others that pretty much used the same code from 2009 and 2010.) Apart from the distressing fact that NOT ONE of my queries is producing anything, my output when I navigate to mysite.com/sitemap.xml is this:
http://www.site.com/ daily 1.0 http://www.site.com/gulf-coast-finest/3 2012-10-01T19:00:00Z weekly 0.9 http://www.site.com/gulf-coast-finest/4 2012-10-01T19:00:00Z weekly 0.9
When I 'view source' I get correctly formatted xml (with the exception that I am missing crucial data), like below:
<?xml version="1.0" encoding="UTF-8" ?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.site.com/</loc>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<!-- posts-->
<url>
<loc>http://www.site.com/gulf-coast-finest/3</loc>
<lastmod>2012-10-01T19:00:00Z</lastmod>
<changefreq>weekly</changefreq>
<priority>0.9</priority>
</url>
<url>
<loc>http://www.site.com/gulf-coast-finest/4</loc>
<lastmod>2012-10-01T19:00:00Z</lastmod>
<changefreq>weekly</changefreq>
<priority>0.9</priority>
</url>
</urlset>
....
I am tearing my hair out wondering why there is no whitespace or xml tags unless I view source. Here is the relevant code from my routes file:
Router::parseExtensions('xml');
Router::connect('/sitemap', array('controller' => 'sitemaps', 'action' => 'index'));
my app/views/layouts/xml/default.ctp file:
<?php
header("content-type: text/xml");
echo $this->Xml->header();
echo $content_for_layout;
?>
my app/views/sitemaps/xml/index.ctp file:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc><?php echo Router::url('/',true); ?></loc>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<!-- posts-->
<?php foreach ($static as $s):?>
<url>
<loc><?php echo Router::url('/gulf-coast-finest/'.$s['Article']['Article.link_txt'].'/'.$s['Article']['id'],true); ?></loc>
<lastmod><?php echo $time->toAtom('2012-10-01 19:00:00'); ?></lastmod>
<changefreq>weekly</changefreq>
<priority>0.9</priority>
</url>
<?php endforeach; ?>
</urlset>
and my obviously completed messed up controller:
<?php
class SitemapsController extends AppController{
var $name = 'Sitemaps';
var $uses = array('Category', 'Listing', 'Article', 'Location');
var $helpers = array('Time');
var $components = array('RequestHandler');
function beforeFilter() {
//debug logs will destroy xml format, make sure were not in drbug mode
Configure::write ('debug', 0);
}
function index (){
$this->RequestHandler->respondAs('xml');
$this->viewPath .= '/xml';
$this->layoutPath = 'xml';
$a=$this->Article->find('all', array('fields'=>array('Article.id', 'Article.link_txt')));
$this->set('static', $a);
}
}
?>
The very important missing data issue is probably suited to another question. The XML file being one line and having no tags visible to humans is not something I'm ok with. I want to know why it's doing this
回答1:
I've the same problem, and i think I've found the solution. I think you've space in front of your HTML / XML code
<-- space here--><?xml version="1.0" encoding="UTF-8" ?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
you can try to find in your sitemap_controller.php if you've a space after PHP closing tag
?>__space here__
来源:https://stackoverflow.com/questions/12684795/unable-to-write-xml-sitemap-in-cakephp