How to represent null value as empty element with JAXB?

前端 未结 4 779
[愿得一人]
[愿得一人] 2020-12-01 06:58

My XSD structure is like the below:-


         


        
4条回答
  •  悲哀的现实
    2020-12-01 07:45

    Blaise's answer is good but it is outdated. There is a much better and simpler method to achieve the same. I have searched many forums and combined different solutions to get to this. I am sharing here so that it will be helpful for others.


    Note: The below solution is more general case than just for date.


    Method - 1 : If you want to replace all null values with empty string in xml

    Session Event Adapter Class

    Add the below class to a convenient package in your code.

    package com.dev
    
    import org.eclipse.persistence.descriptors.ClassDescriptor;
    import org.eclipse.persistence.mappings.DatabaseMapping;
    import org.eclipse.persistence.oxm.mappings.XMLDirectMapping;
    import org.eclipse.persistence.oxm.mappings.nullpolicy.XMLNullRepresentationType;
    import org.eclipse.persistence.sessions.*;
    
    public class NullPolicySessionEventListener extends SessionEventAdapter {
    
    @Override
    public void preLogin(SessionEvent event) {
        Project project = event.getSession().getProject();
        for(ClassDescriptor descriptor : project.getOrderedDescriptors()) {
            for(DatabaseMapping mapping : descriptor.getMappings()) {
                if(mapping.isAbstractDirectMapping()) {
                    XMLDirectMapping xmlDirectMapping = (XMLDirectMapping) mapping;
                    xmlDirectMapping.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.EMPTY_NODE);
                    xmlDirectMapping.getNullPolicy().setNullRepresentedByEmptyNode(true);
                }
            }
        }
     }
    

    }

    Entity Class

    package com.dev;
    
    import javax.xml.bind.annotation.*;
    
    import org.eclipse.persistence.oxm.annotations.*;
    
    @XmlRootElement(name = "Entity")
    public class Entity {
    
        @XmlElement(name = "First_Name", required=true, nillable = true)
        private String firstName;
    
        @XmlElement(name = "Last_Name" , required=true, nillable = true)
        private String lastName;
    
        public Entity(){}
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
    }
    

    DemoApp Class

    package com.dev;
    
    import javax.xml.bind.*;
    import org.eclipse.persistence.*;
    import java.util.Map;
    import java.util.HashMap;
    
    public class DemoApp {
    
    public static void main(String[] args) throws Exception {
        Map properties = new HashMap(1);
        SessionEventListener sessionEventListener = new NullSessionEventListener();
        properties.put(JAXBContextProperties.SESSION_EVENT_LISTENER, sessionEventListener);
        JAXBContext context = JAXBContextFactory.createContext(new Class[] {ListofEntities.class, list.get(0).getClass()},properties);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    
        Entity entity = new Entity();
        entity.setfirstName(null);
        entity.setLastName(null);
        marshaller.marshal(entity, System.out);
    
        entity.setfirstName("Ramu");
        entity.setLastName("K");
        marshaller.marshal(entity, System.out);
    }
    

    }

    Output:

    
    
       
       
    
    
    
       Ramu    
       Ramu   
    
    

    Method - 2 : If you want to replace only selected null values with empty string in xml

    Entity Class

    package com.dev;
    
    import javax.xml.bind.annotation.*;
    
    import org.eclipse.persistence.oxm.annotations.*;
    
    @XmlRootElement(name = "Entity")
    public class Entity {
    
        @XmlElement(name = "First_Name", required=true, nillable = true)
        @XmlNullPolicy(emptyNodeRepresentsNull = true, nullRepresentationForXml = XmlMarshalNullRepresentation.EMPTY_NODE)
        private String firstName;
    
        @XmlElement(name = "Last_Name" , required=true, nillable = true)
        private String lastName;
    
        public Entity(){}
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
    }
    

    DemoApp Class

    package com.dev;
    
    import javax.xml.bind.*;
    import org.eclipse.persistence.*;
    
    public class DemoApp {
    
    public static void main(String[] args) throws Exception {
        JAXBContext context = JAXBContextFactory.createContext(new Class[] {ListofEntities.class, list.get(0).getClass()},null);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    
        Entity entity = new Entity();
        entity.setfirstName(null);
        entity.setLastName(null);
        marshaller.marshal(entity, System.out);
    
        entity.setfirstName("Ramu");
        entity.setLastName("K");
        marshaller.marshal(entity, System.out);
    }
    

    }

    Output:

    In this output, we see only the element with XmlNullPolicy annotation is shown when value is null. The other element is omitted because of default behavior of jaxb.

    
    
       
    
    
    
       Ramu    
       Ramu   
    
    

    References:

    1. Where to include jaxb.properties file?

    2. Represent null value as empty element in xml jaxb

提交回复
热议问题