为什么以及何时在JAXB中需要JAXBElement? [英] Why and when JAXBElement is required in JAXB?
问题描述
我刚刚学习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 :
- 需要
JAXBElement
包装费用
? - 何时使用
JAXBElement
?
- What is the need of
JAXBElement
wrapper aroundexpense
? - when to use
JAXBElement
?
推荐答案
有一些用例需要 JAXBElement
:
- 元素是
nillable =true
和minOccurs =0
。在这种情况下,映射字段/属性上的null
是什么意思?当属性为JAXBElement
时,空值表示该元素不存在且JAXBElement
包装null表示XML元素使用xsi:nil =true
。 - 有2个全局元素具有相同的命名复杂类型。由于在JAXB类中对应于复杂类型,因此需要一种方法来捕获遇到的根元素。
- An element is both
nillable="true"
andminOccurs="0"
. In this case what doesnull
on the mapped field/property mean? When the property isJAXBElement
a null value means the element isn't present and aJAXBElement
wrapping null means an XML element withxsi:nil="true"
. - 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屋!