简体   繁体   English

无法使用JMS序列化程序将空数组序列化为空XML元素

[英]Unable to serialize empty array to empty XML element with JMS serializer

I am assembling an XML file based on an XSD that requires an XML container element to be present, even if it is empty. 我正在组装一个基于XSD的XML文件,该文件需要存在XML容器元素,即使它是空的。

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. 当我尝试使用JMS序列化程序序列化一个空数组时,如果数组不为空,则配置可行,我根本就没有元素。

Can I resolve this by configuration or will I have to implement my own event handler? 我可以通过配置来解决这个问题,还是我必须实现自己的事件处理程序?

Thanks in advance. 提前致谢。

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 . 我做了一些挖掘,结果发现有一个未记录的选项可以在xml_list上指定,名为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 在xsd2php中也实现了对上述属性的支持,以及几天前合并到master中的以下PR: https//github.com/goetas-webservices/xsd2php/pull/27

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 这里setSerializeNull(true)将强制属性/属性序列化,即使它是null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM