Jaxb: How do I generate ObjectFactory class?
Asked Answered
T

3

8

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?

Therese answered 22/6, 2011 at 15:19 Comment(0)
B
12

UPDATE

This question may be referring to the role of ObjectFactory in creating a JAXBContext. If you bootstrap a JAXBContext on 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 ObjectFactory but still wish to create you JAXBContext on a context path you can include a file called jaxb.index in that package listing files to be included in the JAXBContext (referenced classes will automatically pulled in):

Alternatively you can bootstrap you JAXBContext on an array of classes instead of a context path:


Is ObjectFactory Required

An ObjectFactory is not required, although even when starting from Java classes there are use cases where you can leverage a similar class annotated with @XmlRegistry in order to use the @XmlElementDecl annotation.

Creating an Instance of JAXBElement

You can always create the JAXBElement directly:

final JAXBElement<WebLeads> webLeadsElement = new JAXBElement<WebLeads>(
    new QName("root-element-name"), 
    WebLeads.class, 
    webLeadsJavaObj);

Alternative to JAXBElement

Or since JAXBElement is simply used to provide root element information, you can annotate your WebLeads class with @XmlRootElement:

@XmlRootElement(name="root-element-name")
public class WebLeads {
   ...
}
Basia answered 22/6, 2011 at 16:0 Comment(3)
Regarding your "Alternatie to JAXBElement" comment, how do I output the XML? I added the root element, per your direction, but the call "marshaller.marshal( webLeads, writer );" results in a 'javax.xml.bind.JAXBException: "com.myco.systems.leadsmonitor.domain" doesnt contain ObjectFactory.class or jaxb.index' exception.Therese
@Therese - How are you bootstrapping your JAXBContext? If you are bootstrapping on a context path (String) then you will need to include a file called jaxb.index in the same package as your domain classes with a carriage return list of class names (for an example see: bdoughan.blogspot.com/2010/08/…), or you could create your JAXBContext on an array of classes (for an example see: bdoughan.blogspot.com/2010/11/…).Basia
I had been bootstrapping by creating a new instance using the package name, but when I changed it to create a new context based on the class everything worked. Your links were really helpful. Thanks, -Therese
P
3

I don't think you need an ObjectFactory.

It's just a utility class XJC generates to make life easier in some cases.

Edit: Reading your question, I guess you created the POJOs with JAXB annotations by hand.

Consider to add the XmlRootElement on the "root" class: http://download.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/XmlRootElement.html

Here some more info: No @XmlRootElement generated by JAXB

Procrustean answered 22/6, 2011 at 15:23 Comment(1)
Is it possible to suppress ObjectFactory creation in XJC?Schweinfurt
T
2

You don't 'need' a factory for the JaxB marshaller to function. If you pass it an object with a list or a map variable, it will in fact marshall it correctly. This is of course true only if you've correctly initilized the JaxB marshaller towards the object's class that you want to marshall.

You can create a factory, and that factory can create some specialized return (say you don't want it to return your public temp variables)

Tensive answered 22/6, 2011 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.