10000 [Serializer] [XML] Ignore Process Instruction · symfony/symfony@0c741f5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c741f5

Browse files
committed
[Serializer] [XML] Ignore Process Instruction
1 parent ab1d938 commit 0c741f5

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
@@ -371,6 +371,44 @@ public function testDecodeArray()
371371
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
372372
}
373373

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

0 commit comments

Comments
 (0)
0