DEV Community

Eelco Verbrugge
Eelco Verbrugge

Posted on • Edited on

8

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~

Jetbrains image

Don’t Become a Data Breach Headline

57% of organizations have suffered from a security incident related to DevOps toolchain exposures. Is your CI/CD protected? Check out these nine practical tips to keep your CI/CD secure—without adding friction.

Learn more

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay