afsane
afsane

Reputation: 1919

save xml file on zend

this is my code . but I don't know why this code can't create xml file and don`t show any error !!

it shows xml result perfectly but can`t save this file on determined path!!

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);

    }

}

these 2 functions are in a controller class

Upvotes: 0

Views: 2048

Answers (2)

Thargor
Thargor

Reputation: 1872

What returns

$this->view->serverUrl()

I think it is something like http://www.xxxx.xx, because it returns a URL.

Is this maybe the problem? You need something like

$xml->save("foo.xml");

Upvotes: 1

DaveRandom
DaveRandom

Reputation: 88657

$xml->save($this->view->serverUrl() . "/sitemap.xml");

You are attempting to save to a full HTTP URL. Unless your server supports PUT method uploads (which I doubt, and that is a good thing) this will not work.

DOMDocument::saveXML() expects a local file system path. Since you seem to want to save this in your document root, I suspect that simply:

$xml->save("sitemap.xml");

...will do the job.

Upvotes: 0

Related Questions