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

Skip to content

Commit 2a5427a

Browse files
committed
[Serializer] [XML] Ignore Process Instruction
1 parent ab1d938 commit 2a5427a

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-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: 43 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,41 @@ public function testDecodeEmptyXml()
465473
$this->encoder->decode(' ', 'xml');
466474
}
467475

476+
private function getXmlPISource()
477+
{
478+
return <<<'XML'
479+
<?xml version="1.0"?>
480+
<?xml-stylesheet type="text/xsl" href="/xsl/xmlverbatimwrapper.xsl"?>
481+
<?display table-view?>
482+
<?sort alpha-ascending?>
483+
<response>
484+
<foo>foo</foo>
485+
<?textinfo whitespace is allowed ?>
486+
<bar>a</bar>
487+
<bar>b</bar>
488+
<baz>
489+
<key>val</key>
490+
<key2>val</key2>
491+
<item key="A B">bar</item>
492+
<item>
493+
<title>title1</title>
494+
</item>
495+
<?item ignore-title ?>
496+
<item>
497+
<title>title2</title>
498+
</item>
499+
<Barry>
500+
<FooBar id="1">
501+
<Baz>Ed</Baz>
502+
</FooBar>
503+
</Barry>
504+
</baz>
505+
<qux>1</qux>
506+
</response>
507+
<?instruction <value> ?>
508+
XML;
509+
}
510+
468511
protected function getXmlSource()
469512
{
470513
return '<?xml version="1.0"?>'."\n".

0 commit comments

Comments
 (0)