Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22be529

Browse files
committed
[Serializer] [XML] Ignore Process Instruction
1 parent ab1d938 commit 22be529

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,16 @@ public function decode($data, $format, array $context = array())
9292
throw new UnexpectedValueException($error->message);
9393
}
9494

95+
$rootNode = null;
9596
foreach ($dom->childNodes as $child) {
9697
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
9798
throw new UnexpectedValueException('Document types are not allowed.');
9899
}
100+
if (!$rootNode && $child->nodeType !== XML_PI_NODE) {
101+
$rootNode = $child;
102+
}
99103
}
100104

101-
$rootNode = $dom->firstChild;
102-
103105
// todo: throw an exception if the root node name is not correctly configured (bc)
104106

105107
if ($rootNode->hasChildNodes()) {
@@ -329,6 +331,10 @@ private function parseXmlValue(\DOMNode $node)
329331
$value = array();
330332

331333
foreach ($node->childNodes as $subnode) {
334+
if ($subnode->nodeType === XML_PI_NODE) {
335+
continue;
336+
}
337+
332338
$val = $this->parseXml($subnode);
333339

334340
if ('item' === $subnode->nodeName && isset($val['@key'])) {

src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,14 @@ public function testDecodeArray()
371371
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
372372
}
373373

374+
public function testDecodeXMLWithProcessInstruction()
375+
{
376+
$source = $this->getXmlPISource();
377+
$obj = $this->getObject();
378+
379+
$this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
380+
}
381+
374382
public function testDecodeIgnoreWhiteSpace()
375383
{
376384
$source = <<<'XML'
@@ -465,6 +473,23 @@ public function testDecodeEmptyXml()
465473
$this->encoder->decode(' ', 'xml');
466474
}
467475

476+
protected function getXmlPISource()
477+
{
478+
return '<?xml version="1.0"?>'."\n".
479+
'<?xml-stylesheet type="text/xsl" href="/xsl/xmlverbatimwrapper.xsl"?>'.
480+
'<?display table-view?>'.
481+
'<?sort alpha-ascending?>'.
482+
'<response>'.
483+
'<foo>foo</foo>'.
484+
'<?textinfo whitespace is allowed ?>'.
485+
'<bar>a</bar><bar>b</bar>'.
486+
'<baz><key>val</key><key2>val</key2><item key="A B">bar</item>'.
487+
'<item><title>title1</title></item><?item ignore-title ?><item><title>title2</title></item>'.
488+
'<Barry><FooBar id="1"><Baz>Ed</Baz></FooBar></Barry></baz>'.
489+
'<qux>1</qux>'.
490+
'</response><?instrtuction <value> ?>'."\n";
491+
}
492+
468493
protected function getXmlSource()
469494
{
470495
return '<?xml version="1.0"?>'."\n".

0 commit comments

Comments
 (0)