简体   繁体   English

列表为空时的XStream空标记

[英]XStream empty tag when List is empty

When using XStream, I get am empty tag(on the collection field) if I try to serialise an Object has java.util.List collection which is empty. 当使用XStream时,如果我尝试序列化一个对象的java.util.List集合为空,则我将获得一个空标记(在集合字段上)。 How to I remove that empty tag in the xml output? 如何删除xml输出中的空标记?

Your List variable must be null and not containing an empty list to avoid the 'empty' tag. 您的List变量必须为null,并且不包含空列表,以避免使用'empty'标记。

List list = getList();
if(list.isEmpty()) { list = null; }

// Serialization ...

EDIT: 编辑:

If you want that to be done automatically you need to create a custom converter, follow this tutorial: http://x-stream.github.io/converter-tutorial.html 如果您希望自动完成此操作,则需要创建一个自定义转换器,请按照以下教程操作: http : //x-stream.github.io/converter-tutorial.html

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

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