简体   繁体   中英

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.

Instead of representing xml in below format

</person>

Want to represent like this

<person></person >

I am using java 8 and JAXB

This is kind of hacky way to do it, but it works:

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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