栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用JAXB支持具有微小变化的模式

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用JAXB支持具有微小变化的模式

注意: 我是 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>

演示版

下面的示例代码演示了如何指定外部元数据。请注意,我是如何引入第四家供应商来表明可以组合外部元数据文档的。

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>

想要查询更多的信息

  • http://blog.bdoughan.com/search/label/jaxb.properties
  • http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
  • http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/453625.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号