-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
xml.php
167 lines (153 loc) · 4.36 KB
/
xml.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/* SVN FILE: $Id$ */
/**
* XML Helper class file.
*
* Simplifies the output of XML documents.
*
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs.view.helpers
* @since CakePHP(tm) v 1.2
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
App::import('Core', array('Xml', 'Set'));
/**
* XML Helper class for easy output of XML structures.
*
* XmlHelper encloses all methods needed while working with XML documents.
*
* @package cake
* @subpackage cake.cake.libs.view.helpers
*/
class XmlHelper extends AppHelper {
/**
* Default document encoding
*
* @access public
* @var string
*/
var $encoding = 'UTF-8';
/**
* Constructor
* @return void
*/
function __construct() {
parent::__construct();
$this->Xml =& new Xml();
$this->Xml->options(array('verifyNs' => false));
}
/**
* Returns an XML document header
*
* @param array $attrib Header tag attributes
* @return string XML header
*/
function header($attrib = array()) {
if (Configure::read('App.encoding') !== null) {
$this->encoding = Configure::read('App.encoding');
}
if (is_array($attrib)) {
$attrib = array_merge(array('encoding' => $this->encoding), $attrib);
}
if (is_string($attrib) && strpos($attrib, 'xml') !== 0) {
$attrib = 'xml ' . $attrib;
}
return $this->output($this->Xml->header($attrib));
}
/**
* Adds a namespace to any documents generated
*
* @param string $name The namespace name
* @param string $url The namespace URI; can be empty if in the default namespace map
* @return boolean False if no URL is specified, and the namespace does not exist
* default namespace map, otherwise true
* @deprecated
* @see Xml::addNs()
*/
function addNs($name, $url = null) {
return $this->Xml->addNamespace($name, $url);
}
/**
* Removes a namespace added in addNs()
*
* @param string $name The namespace name or URI
* @deprecated
* @see Xml::removeNs()
*/
function removeNs($name) {
return $this->Xml->removeGlobalNamespace($name);
}
/**
* Generates an XML element
*
* @param string $name The name of the XML element
* @param array $attrib The attributes of the XML element
* @param mixed $content XML element content
* @param boolean $endTag Whether the end tag of the element should be printed
* @return string XML
*/
function elem($name, $attrib = array(), $content = null, $endTag = true) {
$namespace = null;
if (isset($attrib['namespace'])) {
$namespace = $attrib['namespace'];
unset($attrib['namespace']);
}
$cdata = false;
if (is_array($content) && isset($content['cdata'])) {
$cdata = true;
unset($content['cdata']);
}
if (is_array($content) && array_key_exists('value', $content)) {
$content = $content['value'];
}
$children = array();
if (is_array($content)) {
$children = $content;
$content = null;
}
$elem =& $this->Xml->createElement($name, $content, $attrib, $namespace);
foreach ($children as $child) {
$elem->createElement($child);
}
$out = $elem->toString(array('cdata' => $cdata, 'leaveOpen' => !$endTag));
if (!$endTag) {
$this->Xml =& $elem;
}
return $this->output($out);
}
/**
* Create closing tag for current element
*
* @return string
*/
function closeElem() {
$name = $this->Xml->name();
if ($parent =& $this->Xml->parent()) {
$this->Xml =& $parent;
}
return $this->output('</' . $name . '>');
}
/**
* Serializes a model resultset into XML
*
* @param mixed $data The content to be converted to XML
* @param array $options The data formatting options. For a list of valid options, see
* XmlNode::__construct().
* @return string A copy of $data in XML format
* @see XmlNode
*/
function serialize($data, $options = array()) {
$options += array('attributes' => false, 'format' => 'attributes');
$data =& new Xml($data, $options);
return $data->toString($options + array('header' => false));
}
}
?>