Search code examples
javaxmljaxbmoxy

Add custom values to external binding (JAXB - MOXY implementation)


I want to marshal a Java-Object to XML.For this approach I am using JAXB Moxy with an external XML binding file.

Here is an example class which I want marshal to XML:

public class Customer {
  private String lastname;
  private String firstname;

  //getters and setters
}

With my external binding file I can access the values of this class, so that I get the following XML-output:

<?xml version="1.0"?>
<customer>
  <firstname>Tony</firstname>
  <lastname>Stark</lastname>
</customer>

Now I want to add a custom tag with a custom value which isn't specified in the java class. So for the above Customer class I want an XML output like this:

<?xml version="1.0"?>
<customer>
  <firstname>Tony</firstname>
  <lastname>Stark</lastname>
  <birthdate>01.01.1990</birthdate>
</customer>

Birthdate isn't in the Customer-class and I don't want to add it there because this class is automaticaly generated by a script. My goal is to define birthdate with my custom value in the external binding file. Is this even possible with the JAXB MOXY implementation? Hope someone can help me.


Solution

  • Below is a way that you could leverage an XmlAdapter to do this:

    XmlAdapter (LastNameAdapter)

    import javax.xml.bind.annotation.XmlType;
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class LastNameAdapter extends XmlAdapter<LastNameAdapter.AdaptedLastName, String> {
    
        @XmlType(propOrder={"lastname", "birthdate"})
        public static class AdaptedLastName {
            public String lastname;
            public String birthdate;
        }
    
        private String birthdate;
    
        public LastNameAdapter() {
        }
    
        public LastNameAdapter(String birthdate) {
            this.birthdate = birthdate;
        }
    
        @Override
        public String unmarshal(AdaptedLastName v) throws Exception {
            return v.lastname;
        }
    
        @Override
        public AdaptedLastName marshal(String v) throws Exception {
            AdaptedLastName adaptedLastName = new AdaptedLastName();
            adaptedLastName.lastname = v;
            adaptedLastName.birthdate = birthdate;
            return adaptedLastName;
        }
    
    }
    

    External Metadata (oxm.xml)

    <?xml version="1.0"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="forum19641824">
        <java-types>
            <java-type name="Customer">
                <xml-root-element/>
                <xml-type prop-order="firstname lastname"/>
                <java-attributes>
                    <xml-element java-attribute="firstname"/>
                    <xml-element java-attribute="lastname" xml-path=".">
                        <xml-java-type-adapter value="forum19641824.LastNameAdapter"/>
                    </xml-element>
                </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    Demo

    import java.util.*;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            Map<String, Object> properties = new HashMap<String, Object>(1);
            properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum19641824/oxm.xml");
            JAXBContext jc = JAXBContext.newInstance(new Class[] {Customer.class}, properties);
    
            Customer customer = new Customer();
            customer.setFirstname("Tony");
            customer.setLastname("Stark");
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setAdapter(new LastNameAdapter("01.01.1990"));
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(customer, System.out);
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8"?>
    <customer>
       <firstname>Tony</firstname>
       <lastname>Stark</lastname>
       <birthdate>01.01.1990</birthdate>
    </customer>