Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
You could use the external binding document in EclipseLink JAXB to map the variations among XML Schemas.
Vendor 1
You could use the standard JAXB annotations to map one of the vendors:
package forum9419732;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlAttribute
private int id;
private String lastName;
private String firstName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
Vendor 2
We will use MOXy's external metadata to customize the metadata provided by annotations. In the document (oxm-v2.xml
) below we will map the firstName
and lastName
properties to XML Attributes:
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum9419732">
<java-types>
<java-type name="Customer">
<java-attributes>
<xml-attribute java-attribute="firstName"/>
<xml-attribute java-attribute="lastName"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
Vendor 3
Again we will use MOXy's external binding document (oxm-v3.xml) to override the annotations. This time we will make the id
property an XML element.
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum9419732">
<java-types>
<java-type name="Customer">
<java-attributes>
<xml-element java-attribute="id" name="identifier"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
Demo
The sample code below demonstrates to specify the external metadata. Note how I introduced a fourth vendor to show that the external metadata documents can be combined.
package forum9419732;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
public class Demo {
public static void main(String[] args) throws JAXBException {
Customer customer = new Customer();
customer.setId(123);
customer.setFirstName("Jane");
customer.setLastName("Doe");
// VENDOR 1
JAXBContext jcV1 = JAXBContext.newInstance(Customer.class);
marshal(jcV1, customer);
// VENDOR 2
Map<String, Object> propertiesV2 = new HashMap<String, Object>(1);
propertiesV2.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum9419732/oxm-v2.xml");
JAXBContext jcV2 = JAXBContext.newInstance(new Class[] {Customer.class}, propertiesV2);
marshal(jcV2, customer);
// VENDOR 3
Map<String, Object> propertiesV3 = new HashMap<String, Object>(1);
propertiesV3.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum9419732/oxm-v3.xml");
JAXBContext jcV3 = JAXBContext.newInstance(new Class[] {Customer.class}, propertiesV3);
marshal(jcV3, customer);
// VENDOR 4
Map<String, Object> propertiesV4 = new HashMap<String, Object>(1);
List<String> oxmV4 = new ArrayList<String>(2);
oxmV4.add("forum9419732/oxm-v2.xml");
oxmV4.add("forum9419732/oxm-v3.xml");
propertiesV4.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, oxmV4);
JAXBContext jcV4 = JAXBContext.newInstance(new Class[] {Customer.class}, propertiesV4);
marshal(jcV4, customer);
}
private static void marshal(JAXBContext jc, Customer customer) throws JAXBException {
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(customer, System.out);
System.out.println();
}
}
Output
Below is the output from each of the vendors. Remember that the same instance of Customer
was used to make each of these XML documents.
<?xml version="1.0" encoding="UTF-8"?>
<customer id="123">
<lastName>Doe</lastName>
<firstName>Jane</firstName>
</customer>
<?xml version="1.0" encoding="UTF-8"?>
<customer id="123" lastName="Doe" firstName="Jane"/>
<?xml version="1.0" encoding="UTF-8"?>
<customer>
<identifier>123</identifier>
<lastName>Doe</lastName>
<firstName>Jane</firstName>
</customer>
<?xml version="1.0" encoding="UTF-8"?>
<customer lastName="Doe" firstName="Jane">
<identifier>123</identifier>
</customer>
For More Information