为什么以及何时在JAXB中需要JAXBElement? [英] Why and when JAXBElement is required in JAXB?

查看:1223
本文介绍了为什么以及何时在JAXB中需要JAXBElement?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚学习JAXB(用于XML绑定的Java体系结构)。通过一些消息来源,我想到了一个关于 JAXBElement 的疑问。


Oracle文档说:

I am just learning JAXB (Java Architecture for XML Binding). Reading through a few sources , one doubt has come in my mind regarding JAXBElement.

Oracle docs say:

当XML元素信息不能通过XML内容的派生Java表示推断,提供了JAXBElement对象。此对象具有获取和设置对象名称和对象值的方法。 此处链接

这是否意味着需要在那里使用 JAXBElement 是不是Schema定义的数据类型和Java数据类型之间的直接映射?

Does it mean that JAXBElement needs to be used when there is not a direct mapping between Schema defined datatype and Java data type?

此外,在下面列出的一个代码示例中。我跟随这里

Further, In one of the code examples listed under. which i followed from here :

 ObjectFactory factory = new ObjectFactory();

    UserT user = factory.createUserT();
    user.setUserName("Sanaulla");
    ItemT item = factory.createItemT();
    item.setItemName("Seagate External HDD");
    item.setPurchasedOn("August 24, 2010");
    item.setAmount(new BigDecimal("6776.5"));

    ItemListT itemList = factory.createItemListT();
    itemList.getItem().add(item);

    ExpenseT expense = factory.createExpenseT();// we get expense object here
    expense.setUser(user);
    expense.setItems(itemList);

    JAXBContext context = JAXBContext.newInstance("generated");
    JAXBElement<ExpenseT> element = factory.createExpenseReport(expense);//why is this required
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty("jaxb.formatted.output",Boolean.TRUE);
    marshaller.marshal(element,System.out);

使用 ExpenseT费用= factory.createExpenseT(); 我们能够获得 ExpenseT 对象。


如果我们看到,我们再次在代码中创建了
的JAXBElement< ExpenseT> element = factory.createExpenseReport(expense);
根据此源费用对象的包装。
另一方面,我们不为对象创建包装器使用
检索UserT用户= factory.createUserT();



所以我的问题是:

Using ExpenseT expense = factory.createExpenseT(); we are able to get ExpenseT object.

Again in the code if we see ,we create

JAXBElement<ExpenseT> element = factory.createExpenseReport(expense);
which according to this source is a wrapper for expense object.
On the other hand we don't create wrappers for the objects retrieved using
UserT user = factory.createUserT();

So my questions are :


  1. 需要 JAXBElement 包装费用

  2. 何时使用 JAXBElement

  1. What is the need of JAXBElement wrapper around expense ?
  2. when to use JAXBElement ?


推荐答案

有一些用例需要 JAXBElement


  1. 元素是 nillable =true minOccurs =0 。在这种情况下,映射字段/属性上的 null 是什么意思?当属性为 JAXBElement 时,空值表示该元素不存在且 JAXBElement 包装null表示XML元素使用 xsi:nil =true

  2. 有2个全局元素具有相同的命名复杂类型。由于在JAXB类中对应于复杂类型,因此需要一种方法来捕获遇到的根元素。
  1. An element is both nillable="true" and minOccurs="0". In this case what does null on the mapped field/property mean? When the property is JAXBElement a null value means the element isn't present and a JAXBElement wrapping null means an XML element with xsi:nil="true".
  2. There are 2 global elements with the same named complex type. Since in JAXB classes correspond to complex types a way is needed to capture which root element was encountered.
    • http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html

这篇关于为什么以及何时在JAXB中需要JAXBElement?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆