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:
When XML element information can not be inferred by the derived Java representation of the XML content, a JAXBElement object is provided. This object has methods for getting and setting the object name and object value.
Link here
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);
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 :
- What is the need of
JAXBElement
wrapper aroundexpense
? - when to use
JAXBElement
?