dom = new DOMDocument('1.0','utf-8'); $this->dom->load($this ->template);} //Encapsulate the createChannel method to create the only and necessary channel node public function createChannel() {$channel = $this->dom->createElement('channel'); $channel->appendChild( $this->createEle('title',$this->title)); $channel->appendChild($this->createEle('link',$this->link)); $channel->appendChild($this ->createEle('description',$this->description)); $this->dom->appendChild($channel);} //Call createItem to generate all item nodes public function display() {$this ->createChannel(); header('content-type:text/xml'); echo $this->dom->savexml();} //Encapsulate a method to create item protected function crea teItem($arr) {$item = $this->dom->createElement('item'); foreach ($$arr as $k => $v) {$item->appendChild($this->createEle($ k,$v));} return $item;} //Encapsulate a method and directly create a node such assome text protected function createEle($name,$value) {$ele = $this ->dom->createElement($name); $text = $this->dom->createTextNode($value); $ele->appendChild($text); return $ele; })$feed = new feed() ;$feed->display();?>
The running results are as follows:
< /p>
The feed.xml that will be used is as follows:
< ?xml version="1.0" encoding="utf-8"?>