xml.php
Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 App::import('Core', array('Xml', 'Set'));
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 class XmlHelper extends AppHelper {
00038
00039
00040
00041
00042
00043
00044
00045 var $encoding = 'UTF-8';
00046
00047
00048
00049
00050 function __construct() {
00051 parent::__construct();
00052 $this->Xml =& new Xml();
00053 $this->Xml->options(array('verifyNs' => false));
00054 }
00055
00056
00057
00058
00059
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
00077
00078
00079
00080
00081
00082
00083
00084
00085 function addNs($name, $url = null) {
00086 return $this->Xml->addNamespace($name, $url);
00087 }
00088
00089
00090
00091
00092
00093
00094
00095 function removeNs($name) {
00096 return $this->Xml->removeGlobalNamespace($name);
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
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
00140
00141
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
00152
00153
00154
00155
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 ?>