Skip to content

Commit 86343b4

Browse files
committed
[Serializer] Revert DOMElement to SimpleXmlElement
1 parent b02c1d9 commit 86343b4

File tree

1 file changed

+26
-42
lines changed

1 file changed

+26
-42
lines changed

src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,11 @@ public function encode($data, $format)
5353
*/
5454
public function decode($data, $format)
5555
{
56-
$xml = \DOMDocument::loadXML($data);
57-
if (!$xml->documentElement->hasChildNodes()) {
58-
return "";
59-
} elseif ($xml->documentElement->childNodes->length == 1 && $xml->documentElement->firstChild instanceof \DOMText) {
60-
return trim((string)$xml->documentElement->firstChild->wholeText);
56+
$xml = simplexml_load_string($data);
57+
if (!$xml->count()) {
58+
return (string) $xml;
6159
}
62-
return $this->parseXml($xml->documentElement);
60+
return $this->parseXml($xml);
6361
}
6462

6563
/**
@@ -151,62 +149,48 @@ final protected function isElementNameValid($name)
151149
}
152150

153151
/**
154-
* Parse the input DOMElement into an array
152+
* Parse the input SimpleXmlElement into an array
155153
*
156-
* @param DOMElement $node xml to parse
154+
* @param SimpleXmlElement $node xml to parse
157155
* @return array
158156
*/
159157
private function parseXml($node)
160158
{
161159
$data = array();
162-
foreach ($node->childNodes as $subnode) {
163-
//When xml is "beautiful" (with tabs and newlines...), tabs and newline are considered as text but we do not want them
164-
if ($subnode instanceof DOMText && trim($subnode->wholeText) === "") {
165-
continue;
166-
}
167-
if (!$subnode->hasChildNodes()) {
168-
$value = "";
169-
} elseif ($subnode->childNodes->length == 1 && $subnode->firstChild instanceof \DOMText) {
170-
$value = trim((string)$subnode->firstChild->wholeText);
171-
} else {
160+
foreach ($node->children() as $key => $subnode) {
161+
if ($subnode->count()) {
172162
$value = $this->parseXml($subnode);
173-
}
174-
175-
if ($subnode->hasAttributes()) {
176-
if (is_string($value) && $value !== "") {
177-
$value = array('#' => $value);
178-
} elseif (is_string($value)) {
179-
$value = array();
163+
if ($subnode->attributes()) {
164+
foreach ($subnode->attributes() as $attrkey => $attr) {
165+
$value['@'.$attrkey] = (string) $attr;
166+
}
180167
}
181-
foreach($subnode->attributes as $attrKey => $attr) {
182-
$value['@'.$attrKey] = (string) $attr->value;
168+
} elseif ($subnode->attributes()) {
169+
$value = array();
170+
foreach ($subnode->attributes() as $attrkey => $attr) {
171+
$value['@'.$attrkey] = (string) $attr;
183172
}
173+
$value['#'] = (string) $subnode;
174+
} else {
175+
$value = (string) $subnode;
184176
}
185177

186-
if ($subnode->tagName === 'item') {
178+
if ($key === 'item') {
187179
if (isset($value['@key'])) {
188-
$key = $value['@key'];
189-
$tmp = $value['#'];
190-
unset($value['@key']);
191-
unset($value['#']);
192-
if (!empty($value)) {
193-
$data[$key] = array_merge(array('#' => $tmp), $value);
194-
} else {
195-
$data[$key] = $tmp;
196-
}
180+
$data[(string)$value['@key']] = $value['#'];
197181
} elseif (isset($data['item'])) {
198182
$tmp = $data['item'];
199183
unset($data['item']);
200184
$data[] = $tmp;
201185
$data[] = $value;
202186
}
203-
} elseif (key_exists($subnode->tagName, $data)) {
204-
if ((false === is_array($data[$subnode->tagName])) || (false === isset($data[$subnode->tagName][0]))) {
205-
$data[$subnode->tagName] = array($data[$subnode->tagName]);
187+
} elseif (key_exists($key, $data)) {
188+
if ((false === is_array($data[$key])) || (false === isset($data[$key][0]))) {
189+
$data[$key] = array($data[$key]);
206190
}
207-
$data[$subnode->tagName][] = $value;
191+
$data[$key][] = $value;
208192
} else {
209-
$data[$subnode->tagName] = $value;
193+
$data[$key] = $value;
210194
}
211195
}
212196
return $data;

0 commit comments

Comments
 (0)