这是我的代码。但我不知道为什么这段代码不能创建 xml 文件并且不显示任何错误!
它完美地显示了 xml 结果,但无法将此文件保存在确定的路径上!!
public function indexAction()
{
// XML-related routine - <urlset>
$xml = new DOMDocument('1.0', 'utf-8');
$masterRoot = $xml->createElement('urlset');
$xml->appendChild($masterRoot);
$publicpath = "/public";
$data = array(
array('lastmod'=> date("Y-m-d"),'loc' => $this->view->serverUrl().$publicpath ,'changefreq' =>'daily','priority' =>'1.00'),
array('lastmod'=> date("Y-m-d"),'loc' => $this->view->serverUrl().$publicpath ."/about us" ,'changefreq' =>'daily','priority' =>'0.98'),
array('lastmod'=> date("Y-m-d"),'loc' => $this->view->serverUrl().$publicpath ."/contact us",'changefreq' =>'daily','priority' =>'0.98'),
array('lastmod'=> date("Y-m-d"),'loc' => $this->view->serverUrl().$publicpath ."/useful links",'changefreq' =>'daily','priority' =>'0.98')
);
$this->_url($xml,$masterRoot,$data);
$output = $xml->saveXML();
$xml->save($this->view->serverUrl() . "/sitemap.xml" );
// Both layout and view renderer should be disabled
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
Zend_Layout::getMvcInstance()->disableLayout();
// Setting up headers and body
$this->_response->setHeader('Content-Type', 'text/xml; charset=utf-8')->setBody($output);
}
protected function _url($xml,$masterRoot,$allData)
{
foreach($allData as $data)
{
// <url>
$root = $xml->createElement('url');
$masterRoot->appendChild($root);
//<loc>http://www.example.com/</loc>
$elem = $xml->createElement('loc');
$root->appendChild($elem);
$elemtext = $xml->createTextNode($data['loc']);
$elem->appendChild($elemtext);
//<lastmod>2005-01-01</lastmod>
$elem = $xml->createElement('lastmod');
$root->appendChild($elem);
$elemtext = $xml->createTextNode($data['lastmod']);
$elem->appendChild($elemtext);
//<changefreq>monthly</changefreq>
$elem = $xml->createElement('changefreq');
$root->appendChild($elem);
$elemtext = $xml->createTextNode($data['changefreq']);
$elem->appendChild($elemtext);
//<priority>0.8</priority>
$elem = $xml->createElement('priority');
$root->appendChild($elem);
$elemtext = $xml->createTextNode($data['priority']);
$elem->appendChild($elemtext);
}
}
这两个函数在一个控制器类中