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



  1. 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-&gt;name;
}

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

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

// Issers

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

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

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

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

/**
 * @param mixed $createdAt
 */
public function setCreatedAt($createdAt): void
{
    $this-&gt;createdAt = $createdAt;
}
Enter fullscreen mode Exit fullscreen mode

}

Enter fullscreen mode Exit fullscreen mode



  1. 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-&gt;encoder      = [new XmlEncoder()];
    $this-&gt;normalizer   = [new ObjectNormalizer()];
    $this-&gt;serializer   = new Serializer($this-&gt;normalizer, $this-&gt;encoder);

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

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

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

}

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-&gt;data =   '&lt;?xml version="1.0" encoding="UTF-8"?&gt;
                     &lt;Person&gt;
                         &lt;name&gt;John Doe&lt;/name&gt;
                         &lt;age&gt;88&lt;/age&gt;
                         &lt;sportsperson&gt;true&lt;/sportsperson&gt;
                     &lt;/Person&gt;';

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

    dd($personObject);
}
Enter fullscreen mode Exit fullscreen mode
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)