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

Skip to content

Commit bca4778

Browse files
committed
bug #22044 [Serializer] [XML] Ignore Process Instruction (jordscream)
This PR was merged into the 2.7 branch. Discussion ---------- [Serializer] [XML] Ignore Process Instruction | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22005 | License | MIT | Doc PR | N/A This Pull request ignores Process instruction data in XML for decoding the data. Commits ------- 0c741f5 [Serializer] [XML] Ignore Process Instruction
2 parents 65260bc + 0c741f5 commit bca4778

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,44 @@ public function testDecodeArray()
370370
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
371371
}
372372

373+
public function testDecodeXMLWithProcessInstruction()
374+
{
375+
$source = <<<'XML'
376+
<?xml version="1.0"?>
377+
<?xml-stylesheet type="text/xsl" href="/xsl/xmlverbatimwrapper.xsl"?>
378+
<?display table-view?>
379+
<?sort alpha-ascending?>
380+
<response>
381+
<foo>foo</foo>
382+
<?textinfo whitespace is allowed ?>
383+
<bar>a</bar>
384+
<bar>b</bar>
385+
<baz>
386+
<key>val</key>
387+
<key2>val</key2>
388+
<item key="A B">bar</item>
389+
<item>
390+
<title>title1</title>
391+
</item>
392+
<?item ignore-title ?>
393+
<item>
394+
<title>title2</title>
395+
</item>
396+
<Barry>
397+
<FooBar id="1">
398+
<Baz>Ed</Baz>
399+
</FooBar>
400+
</Barry>
401+
</baz>
402+
<qux>1</qux>
403+
</response>
404+
<?instruction <value> ?>
405+
XML;
406+
$obj = $this->getObject();
407+
408+
$this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
409+
}
410+
373411
public function testDecodeIgnoreWhiteSpace()
374412
{
375413
$source = <<<'XML'

0 commit comments

Comments
 (0)