I’m using Java 6, JaxB 2 and SpringSource Tool Suite (same as Eclipse). I had a couple of Java classes I wrote, from which I used JaxB to generate an XML schema. However, I’m noticing in order to use JaxB’s ability to generate an XML document from Java objects, I need an ObjectFactory.
final Marshaller marshaller = jaxbContext.createMarshaller();
// Here is where I don't have an ObjectFactory defined
final JAXBElement<WebLeads> webLeadsElement
= (new ObjectFactory()).createWebLeads(webLeadsJavaObj);
How can I generate an ObjectFactory without blowing away the classes I already have now?
UPDATE
This question may be referring to the role of
ObjectFactoryin creating aJAXBContext. If you bootstrap aJAXBContexton a context path then it will check for an ObjectFactory in that location in order to determine the classes in that package:If you do not have an
ObjectFactorybut still wish to create youJAXBContexton a context path you can include a file calledjaxb.indexin that package listing files to be included in theJAXBContext(referenced classes will automatically pulled in):Alternatively you can bootstrap you
JAXBContexton an array of classes instead of a context path:Is ObjectFactory Required
An
ObjectFactoryis not required, although even when starting from Java classes there are use cases where you can leverage a similar class annotated with@XmlRegistryin order to use the@XmlElementDeclannotation.Creating an Instance of JAXBElement
You can always create the
JAXBElementdirectly:Alternative to JAXBElement
Or since JAXBElement is simply used to provide root element information, you can annotate your
WebLeadsclass with@XmlRootElement: