diff --git a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php index dc932222e9..fc4cbe5d83 100644 --- a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php @@ -488,6 +488,9 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec $node->appendChild($child); } elseif ($val instanceof \Traversable) { $this->buildXml($node, $val); + } elseif ($val instanceof \DOMNode) { + $child = $this->dom->importNode($val, true); + $node->appendChild($child); } elseif (\is_object($val)) { if (null === $this->serializer) { throw new BadMethodCallException(sprintf('The serializer needs to be set to allow "%s()" to be used with object data.', __METHOD__)); @@ -502,9 +505,6 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec return $this->appendText($node, $val); } elseif (\is_bool($val)) { return $this->appendText($node, (int) $val); - } elseif ($val instanceof \DOMNode) { - $child = $this->dom->importNode($val, true); - $node->appendChild($child); } return true; diff --git a/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php b/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php index f1a8b6ab48..94f2e6c18a 100644 --- a/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php @@ -662,6 +662,20 @@ XML; $this->assertEquals($expectedXml, $actualXml); } + public function testEncodeXmlWithDomNodeValue() + { + $expectedXml = <<<'XML' + +barfoo & bar + +XML; + $document = new \DOMDocument(); + + $actualXml = $this->encoder->encode(['foo' => $document->createTextNode('bar'), 'bar' => $document->createTextNode('foo & bar')], 'xml'); + + $this->assertEquals($expectedXml, $actualXml); + } + public function testEncodeXmlWithDateTimeObjectValue() { $xmlEncoder = $this->createXmlEncoderWithDateTimeNormalizer();