Ответ 1
Примечание. Я EclipseLink JAXB (MOXy) и член экспертной группы JAXB 2 (JSR-222).
Вы можете использовать внешний документ привязки в EclipseLink JAXB для сопоставления вариантов среди XML-схем.
Поставщик 1
Вы можете использовать стандартные аннотации JAXB для сопоставления одного из поставщиков:
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;
}
}
Поставщик 2
Мы будем использовать MOXy внешние метаданные для настройки метаданных, предоставленных аннотациями. В документе (oxm-v2.xml
) ниже мы сопоставим свойства firstName
и lastName
с атрибутами XML:
<?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>
Поставщик 3
Снова мы будем использовать внешний документ привязки MOXy (oxm-v3.xml), чтобы переопределить аннотации. На этот раз мы сделаем свойство id
XML-элементом.
<?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
Пример кода ниже показывает, чтобы указать внешние метаданные. Обратите внимание, как я представил четвертого поставщика, чтобы показать, что внешние документы метаданных могут быть объединены.
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();
}
}
Выход
Ниже представлен вывод каждого из поставщиков. Помните, что один и тот же экземпляр Customer
использовался для создания каждого из этих XML-документов.
<?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>
Дополнительная информация