DEV Community

Eelco Verbrugge
Eelco Verbrugge

Posted on • Updated on

Serialize/Deserialize XML in Symfony

What is a serializer?

A serializer is meant to convert an object to XML (serialize) or the other way around, convert XML to an object (deserialize).

This can also be done to JSON and other formats, therefor have a look at Symfony's documentation. There are third party serializers, such as JML, but for now we will work with Symfony's default serializer.

How to use Symfony's default serializer

1. Install the serializer by composer

composer require symfony/serializer
Enter fullscreen mode Exit fullscreen mode

2. Create a Model (for demo reasons)

<?php

namespace App\Model;

class Person
{
    private $age;
    private $name;
    private $sportsperson;
    private $createdAt;

    // Getters

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @return mixed
     */
    public function getAge()
    {
        return $this->age;
    }

    /**
     * @return mixed
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    // Issers

    public function isSportsperson()
    {
        return $this->sportsperson;
    }

    /**
     * @param mixed $name
     */
    public function setName($name): void
    {
        $this->name = $name;
    }

    /**
     * @param mixed $age
     */
    public function setAge($age): void
    {
        $this->age = $age;
    }

    /**
     * @param mixed $sportsperson
     */
    public function setSportsperson($sportsperson): void
    {
        $this->sportsperson = $sportsperson;
    }

    /**
     * @param mixed $createdAt
     */
    public function setCreatedAt($createdAt): void
    {
        $this->createdAt = $createdAt;
    }


}
Enter fullscreen mode Exit fullscreen mode

3. Create the SerializerController with a method called serializeObject()

<?php

namespace App\Controller;

use App\Model\Person;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

class SerializerController
{

    protected $encoder;
    protected $normalizer;
    protected $serializer;
    protected $data;

    /**
     * @Route("/serialize-object", name="Serialize Object")
     */
    public function serializeObject()
    {
        $this->encoder      = [new XmlEncoder()];
        $this->normalizer   = [new ObjectNormalizer()];
        $this->serializer   = new Serializer($this->normalizer, $this->encoder);

        $person = new Person();
        $person->setName('foo');
        $person->setAge(99);
        $person->setSportsperson(false);

        $xmlContent = $this->serializer->serialize($person, 'xml');

        dd($xmlContent);
    }
}
Enter fullscreen mode Exit fullscreen mode

Outcome

We created an object Person and serialized it to XML which results in a die and dump like this:
Alt Text

4. Add an other method deserializeObject() to deserialize XML to an object

/**
     * @Route("/deserialize-object", name="Deserialize Object")
     */
    public function deserializeObject()
    {
        $this->encoder      = [new XmlEncoder()];
        $this->normalizer   = [new ObjectNormalizer()];
        $this->serializer   = new Serializer($this->normalizer, $this->encoder);

        $this->data =   '<?xml version="1.0" encoding="UTF-8"?>
                         <Person>
                             <name>John Doe</name>
                             <age>88</age>
                             <sportsperson>true</sportsperson>
                         </Person>';

        $personObject = $this->serializer->deserialize($this->data, Person::class, 'xml');

        dd($personObject);
    }
Enter fullscreen mode Exit fullscreen mode

Outcome

This time we converted xml with a Person and some properties, this resulted in a die and dump as follows:
Alt Text

Thats all folks~

Top comments (0)