1

I have successfully been able to serialize objects into XML with Symfony Serializer. But I'm having trouble serializing and deserializing xml into simple types, for the xml shown below.

I've tried several things with XmlEncoder and Normalizer and nothing seems to work.

For example, the xml <response>Hello there</response> needs to be deserialized into a string variable containing "Hello there".

Similarly, an xml array like the following deserializes to ['Hello World', 'Hello', 'Hi'].

<responses>
    <response>Hello World</response>
    <response>Hello </response>
    <response>Hi</response>
</responses>

Is this even possible to achieve with Symfony Serializer, or do I have to write some custom code to handle this?

4
  • have you tried just ... $serializer->deserialize($data, '[]', 'xml') ? Commented Sep 28, 2019 at 7:10
  • and if that doesn't help, try just the serializer with $serializer->decode($data, 'xml'), but the format might differ slightly in both cases. Commented Sep 28, 2019 at 7:16
  • @Jakumi The first solution gives "could not denormalize object of type [], no supporting normalizer found." The second solution seems to be doing the job, thanks! Surprised it's not in the documentation at symfony.com/doc/current/components/serializer.html Commented Sep 28, 2019 at 11:25
  • probably, because it's slightly unusual ;o) Commented Sep 28, 2019 at 11:26

1 Answer 1

2

The serializer does also allow decoding data, which will most likely return an array which might have a usable structure for your use case.

$serializer->decode($data, 'xml')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.