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 App::import('Core', array('Xml', 'Set'));
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class XmlHelper extends AppHelper {
00036
00037
00038
00039
00040
00041
00042 var $encoding = 'UTF-8';
00043
00044
00045
00046
00047 function __construct() {
00048 parent::__construct();
00049 $this->Xml =& new Xml();
00050 $this->Xml->options(array('verifyNs' => false));
00051 }
00052
00053
00054
00055
00056
00057
00058 function header($attrib = array()) {
00059 if (Configure::read('App.encoding') !== null) {
00060 $this->encoding = Configure::read('App.encoding');
00061 }
00062
00063 if (is_array($attrib)) {
00064 $attrib = array_merge(array('encoding' => $this->encoding), $attrib);
00065 }
00066 if (is_string($attrib) && strpos($attrib, 'xml') !== 0) {
00067 $attrib = 'xml ' . $attrib;
00068 }
00069
00070 return $this->output($this->Xml->header($attrib));
00071 }
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 function addNs($name, $url = null) {
00083 return $this->Xml->addNamespace($name, $url);
00084 }
00085
00086
00087
00088
00089
00090
00091
00092 function removeNs($name) {
00093 return $this->Xml->removeGlobalNamespace($name);
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 function elem($name, $attrib = array(), $content = null, $endTag = true) {
00105 $namespace = null;
00106 if (isset($attrib['namespace'])) {
00107 $namespace = $attrib['namespace'];
00108 unset($attrib['namespace']);
00109 }
00110 $cdata = false;
00111 if (is_array($content) && isset($content['cdata'])) {
00112 $cdata = true;
00113 unset($content['cdata']);
00114 }
00115 if (is_array($content) && isset($content['value'])) {
00116 $content = $content['value'];
00117 }
00118 $children = array();
00119 if (is_array($content)) {
00120 $children = $content;
00121 $content = null;
00122 }
00123
00124 $elem =& $this->Xml->createElement($name, $content, $attrib, $namespace);
00125 foreach ($children as $child) {
00126 $elem->createElement($child);
00127 }
00128 $out = $elem->toString(array('cdata' => $cdata, 'leaveOpen' => !$endTag));
00129
00130 if (!$endTag) {
00131 $this->Xml =& $elem;
00132 }
00133 return $this->output($out);
00134 }
00135
00136
00137
00138
00139
00140 function closeElem() {
00141 $name = $this->Xml->name();
00142 if ($parent =& $this->Xml->parent()) {
00143 $this->Xml =& $parent;
00144 }
00145 return $this->output('</' . $name . '>');
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 function serialize($data, $options = array()) {
00157 $options += array('attributes' => false, 'format' => 'attributes');
00158 $data =& new Xml($data, $options);
00159 return $data->toString($options + array('header' => false));
00160 }
00161 }
00162
00163 ?>