xml.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: view_2helpers_2xml_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 /**
00004  * XML Helper class file.
00005  *
00006  * Simplifies the output of XML documents.
00007  *
00008  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00009  * Copyright 2005-2008, Cake Software Foundation, Inc.
00010  *                              1785 E. Sahara Avenue, Suite 490-204
00011  *                              Las Vegas, Nevada 89104
00012  *
00013  * Licensed under The MIT License
00014  * Redistributions of files must retain the above copyright notice.
00015  *
00016  * @filesource
00017  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00018  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00019  * @package         cake
00020  * @subpackage      cake.cake.libs.view.helpers
00021  * @since           CakePHP(tm) v 1.2
00022  * @version         $Revision: 580 $
00023  * @modifiedby      $LastChangedBy: gwoo $
00024  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00025  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00026  */
00027 App::import('Core', array('Xml', 'Set'));
00028 
00029 /**
00030  * XML Helper class for easy output of XML structures.
00031  *
00032  * XmlHelper encloses all methods needed while working with XML documents.
00033  *
00034  * @package     cake
00035  * @subpackage  cake.cake.libs.view.helpers
00036  */
00037 class XmlHelper extends AppHelper {
00038 
00039 /**
00040  * Default document encoding
00041  *
00042  * @access public
00043  * @var string
00044  */
00045     var $encoding = 'UTF-8';
00046 /**
00047  * Constructor
00048  * @return void
00049  */
00050     function __construct() {
00051         parent::__construct();
00052         $this->Xml =& new Xml();
00053         $this->Xml->options(array('verifyNs' => false));
00054     }
00055 /**
00056  * Returns an XML document header
00057  *
00058  * @param  array $attrib Header tag attributes
00059  * @return string XML header
00060  */
00061     function header($attrib = array()) {
00062         if (Configure::read('App.encoding') !== null) {
00063             $this->encoding = Configure::read('App.encoding');
00064         }
00065 
00066         if (is_array($attrib)) {
00067             $attrib = array_merge(array('encoding' => $this->encoding), $attrib);
00068         }
00069         if (is_string($attrib) && strpos($attrib, 'xml') !== 0) {
00070             $attrib = 'xml ' . $attrib;
00071         }
00072 
00073         return $this->output($this->Xml->header($attrib));
00074     }
00075 /**
00076  * Adds a namespace to any documents generated
00077  *
00078  * @param  string  $name The namespace name
00079  * @param  string  $url  The namespace URI; can be empty if in the default namespace map
00080  * @return boolean False if no URL is specified, and the namespace does not exist
00081  *                 default namespace map, otherwise true
00082  * @deprecated
00083  * @see Xml::addNs()
00084  */
00085     function addNs($name, $url = null) {
00086         return $this->Xml->addNamespace($name, $url);
00087     }
00088 /**
00089  * Removes a namespace added in addNs()
00090  *
00091  * @param  string  $name The namespace name or URI
00092  * @deprecated
00093  * @see Xml::removeNs()
00094  */
00095     function removeNs($name) {
00096         return $this->Xml->removeGlobalNamespace($name);
00097     }
00098 /**
00099  * Generates an XML element
00100  *
00101  * @param  string   $name The name of the XML element
00102  * @param  array    $attrib The attributes of the XML element
00103  * @param  mixed    $content XML element content
00104  * @param  boolean  $endTag Whether the end tag of the element should be printed
00105  * @return string XML
00106  */
00107     function elem($name, $attrib = array(), $content = null, $endTag = true) {
00108         $namespace = null;
00109         if (isset($attrib['namespace'])) {
00110             $namespace = $attrib['namespace'];
00111             unset($attrib['namespace']);
00112         }
00113         $cdata = false;
00114         if (is_array($content) && isset($content['cdata'])) {
00115             $cdata = true;
00116             unset($content['cdata']);
00117         }
00118         if (is_array($content) && isset($content['value'])) {
00119             $content = $content['value'];
00120         }
00121         $children = array();
00122         if (is_array($content)) {
00123             $children = $content;
00124             $content = null;
00125         }
00126 
00127         $elem =& $this->Xml->createElement($name, $content, $attrib, $namespace);
00128         foreach ($children as $child) {
00129             $elem->createElement($child);
00130         }
00131         $out = $elem->toString(array('cdata' => $cdata, 'leaveOpen' => !$endTag));
00132 
00133         if (!$endTag) {
00134             $this->Xml =& $elem;
00135         }
00136         return $this->output($out);
00137     }
00138 /**
00139  * Create closing tag for current element
00140  *
00141  * @return string
00142  */
00143     function closeElem() {
00144         $name = $this->Xml->name();
00145         if ($parent =& $this->Xml->parent()) {
00146             $this->Xml =& $parent;
00147         }
00148         return $this->output('</' . $name . '>');
00149     }
00150 /**
00151  * Serializes a model resultset into XML
00152  *
00153  * @param  mixed  $data The content to be converted to XML
00154  * @param  array  $options The data formatting options
00155  * @return string A copy of $data in XML format
00156  */
00157     function serialize($data, $options = array()) {
00158         $data =& new Xml($data, array_merge(array('attributes' => false, 'format' => 'attributes'), $options));
00159         return $data->toString(array_merge(array('header' => false), $options));
00160     }
00161 }
00162 ?>