简体   繁体   English

XML 映射中 null 值的空 XML 标记

[英]empty XML tag for null value in XML mapping

I would want to generate a xml with open and close tag for elements which has null value.我想为具有 null 值的元素生成一个带有打开和关闭标签的 xml。

Instead of representing xml in below format而不是以下面的格式表示 xml

</person>

Want to represent like this想这样表示

<person></person >

I am using java 8 and JAXB我正在使用 java 8 和 JAXB

This is kind of hacky way to do it, but it works:这是一种hacky方式,但它有效:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class SomeRoot {

    private String person;

    private void beforeMarshal(Marshaller marshaller) {
        if(null == person) {
            person = "";
        }
    }

    private void afterMarshal(Marshaller marshaller) {
        if("".equals(person)) {
            person = null;
        }
    }
}

Check this answer for more details/options: Output empty elements检查此答案以获取更多详细信息/选项: Output 空元素

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

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