/Users/mgirouard/Sites/panda-php/trunk/Panda/View/XML.php

Go to the documentation of this file.
00001 <?php 
00002 
00003 require_once 'Panda/View/Abstract.php';
00004 
00014 class Panda_View_XML
00015 extends Panda_View_Abstract
00016 {
00022     protected $document;
00023     
00029     protected $rootNode = 'data';
00030     
00036     protected $formatOutput = true;
00037     
00043     public function setRootNodeName($name)
00044     {
00045         $name = (string) $name;
00046         
00047         if (!empty($name)) {
00048             $this->rootNode = $name;
00049         }
00050     }
00051     
00057     public function setFormatOutput($bool)
00058     {
00059         $this->formatOutput = (bool) $bool;
00060     }
00061     
00067     public function render()
00068     {
00069         $this->document = new DOMDocument;
00070         $this->document->formatOutput = $this->formatOutput;
00071 
00072         $root  = $this->document->createElement($this->rootNode);
00073         $this->getNodes($this->data, $root);
00074 
00075         $this->document->appendChild($root);
00076         return $this->output($this->document->saveXML());
00077     }
00078 
00088     protected function getNodes($struct, DOMNode $parentNode)
00089     {
00090         if ($this->isIteratable($struct)) {
00091             foreach ($struct as $key => $value) {
00092                 /* Numbers don't make good node names, use the parent's */
00093                 if (is_numeric($key)) {
00094                     $key = $parentNode->nodeName;
00095                 }
00096 
00097                 $childNode = $this->document->createElement($key);
00098                 $this->getNodes($value, $childNode);
00099                 $parentNode->appendChild($childNode);
00100             }
00101         }
00102         else {
00103             $nodeValue = $this->document->createTextNode((string)$struct);
00104             $parentNode->appendChild($nodeValue);
00105         }
00106     }
00107     
00114     protected function isIteratable($struct)
00115     {
00116         return is_object($struct) || is_array($struct) || $struct instanceof Iterator;
00117     }
00118 }

Generated on Thu Sep 4 01:56:50 2008 for Panda PHP Components by  doxygen 1.5.4