9

I am assembling an XML file based on an XSD that requires an XML container element to be present, even if it is empty.

When I try to serialize an empty array, using JMS serializer, with configuration that works if the array is not empty, I get no element at all.

Can I resolve this by configuration or will I have to implement my own event handler?

Thanks in advance.

rhummelmose
  • 647
  • 4
  • 15

2 Answers2

3

I did some digging and it turned out that there is an undocumented option that can be specified on xml_list, called skip_when_empty.

Support for the above mentioned property was also implemented in xsd2php with the following PR that was merged into master a few days ago: https://github.com/goetas-webservices/xsd2php/pull/27

rhummelmose
  • 647
  • 4
  • 15
1

Try this:

$serializer = JMS\SerializerBuilder::create();   
$context = JMS\SerializationContext::create()->setSerializeNull(true);           
$serializedString = $serializer->serialize($data, 'xml', $context);

here setSerializeNull(true) will force property/properties to serialize even if it's null

rajatsaurastri
  • 653
  • 3
  • 21