title: Why and when JAXBElement is required in JAXB?
body: <p>I am just learning JAXB (Java Architecture for XML Binding). Reading through a few sources , one doubt has come in my mind regarding <code>JAXBElement</code>. <br/><br/>
  <b>Oracle docs say:</b></p>
  
  <p><code>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.</code> <a href="http://docs.oracle.com/javaee/5/tutorial/doc/bnazq.html#bnazv">Link here</a></p>
  
  <p><b>Does it mean that <code>JAXBElement</code> needs to be used when there is not a direct mapping between Schema defined datatype and Java data type?</b></p>
  
  <p>Further, In one of the code examples listed under. which i followed from <a href="http://www.javacodegeeks.com/2011/02/jaxb-generate-xml-xsd.html">here</a> :</p>
  
  <pre><code> 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&lt;ExpenseT&gt; element = factory.createExpenseReport(expense);//why is this required
      Marshaller marshaller = context.createMarshaller();
      marshaller.setProperty("jaxb.formatted.output",Boolean.TRUE);
      marshaller.marshal(element,System.out);
  </code></pre>
  
  <p>Using <code>ExpenseT expense = factory.createExpenseT();</code> we are able to get <code>ExpenseT</code> object.<br/> <br/>
  Again in the code if we see ,we create <br/><br/><code>JAXBElement&lt;ExpenseT&gt; element = factory.createExpenseReport(expense);</code><br/>which according to <a href="https://jaxb.java.net/tutorial/section_4_1-Building-and-Marshalling-an-XML-Document.html">this source</a> is a wrapper for <code>expense</code> object.<br/>On the other hand we don't create wrappers for the objects retrieved using <br/><code>UserT user = factory.createUserT();</code>
  <br/><br/>
  <strong>So my questions are :</strong> </p>
  
  <ol>
  <li>What is the need of <code>JAXBElement</code> wrapper around <code>expense</code> ? </li>
  <li>when to use <code>JAXBElement</code> ?</li>
  </ol>