Home » Php » JMS serializer, deserialization and xpath

JMS serializer, deserialization and xpath

Posted by: admin October 18, 2018 Leave a comment

Questions:

So I have this sample XML:

<a>
    <bb>
        <b><c>bc1</c></b>
        <b><c>bc2</c></b>
        <b><c>bc3</c></b>
    </bb>
    <cc>
        <bb>
            <b><c>cbc1</c></b>
            <b><c>cbc2</c></b>
            <b><c>cbc3</c></b>
        </bb>
    </cc>
</a>

And this two entities. Parent entity:

<?php
final class A
{
    /**
     * @JMS\Type("array<B>")
     * @JMS\XmlList(entry="b")
     * @JMS\SerializedName("bb")
     */
    private $bb;
    /**
     * @JMS\Type("array<B>")
     * @JMS\XmlList(entry="b")
     * @JMS\SerializedName("cc/bb")
     */
    private $cc;
}

and child entity:

final class B {
    /**
     * @var string
     *
     * @ORM\Column(type="string", length=24)
     * @JMS\Type("string")
     * @JMS\SerializedName("c")
     */
    private $c;
}

Problem is that after deserialization of my xml

    $object = $this->serializer->deserialize($xml, A::class, 'xml');

i got property bb hydrated as i expect, but property cc is empty.

Question is if there is any way to fill that field without intermediary class/entity?

Answers: