How to change the node name of XML document generated by Symfony serializer? I generate XML with this code:
final class AdsController extends AbstractController
{
private SerializerInterface $serializer;
public function __construct(SerializerInterface $serializer)
{
$this->serializer = $serializer;
$this->normalizer = $normalizer;
}
public function __invoke(Request $request): Response
{
$ads = $this->em->findAll();
$rootNode = [
'@id' => 12345,
'#' => $ads
];
$res = $this->serializer->serialize($rootNode, 'xml', [
'xml_format_output' => true,
'xml_encoding' => 'utf-8',
'xml_root_node_name' => 'ads'
]);
return $res;
}
}
And the $res looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<ads id="12345">
<item></item>
</ads>
But how to get something like this:
<?xml version="1.0" encoding="UTF-8"?>
<ads id="12345">
<ad adId="123"></ad>
</ads>
My normalizer looks like this:
class AdNormalizer implements ContextAwareNormalizerInterface
{
public function normalize($topic, string $format = null, array $context = [])
{
$data['adName'] = ...
return $data;
}
public function supportsNormalization($data, string $format = null, array $context = [])
{
return $data instanceof Ad;
}
}