Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jaxb: How do I generate ObjectFactory class?

Tags:

java

jaxb

jaxb2

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?

like image 985
Dave Avatar asked Jun 22 '11 15:06

Dave


People also ask

Is @an objectfactory required to create a JAXBElement?

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. You can always create the JAXBElement directly:

How to generate JAXB classes from XSD?

JAXB: Generate Classes from XSD 1 Requirements 2 XJC command. The JAXB XJC schema binding compiler transforms/binds, a source XML schema (XSD) to a set of JAXB content classes in the Java programming language. 3 XJC in Action: Generating classes form XSD. To see the command XJC in action, we will need an XSD file. ... 4 Conclusion. ...

Why is my JAXB class not generating classes?

the issue isn't your code, the issue is exactly what the exception says. jaxb classes should have a factory class named ObjectFactory in the same package as the classes. if you are generating the classes using xjc, this class will be generated for you. In fact, I am familiar with JAXB and used it in several projects.

Is an objectfactory required for JAXBContext bootstrap?

Alternatively you can bootstrap you JAXBContext on an array of classes instead of a context path: 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.


1 Answers

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:

  • http://bdoughan.blogspot.com/2010/09/processing-atom-feeds-with-jaxb.html

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):

  • http://bdoughan.blogspot.com/2010/08/using-xmlanyelement-to-build-generic.html

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

  • http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-using-xsitype.html

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 {
   ...
}
like image 198
bdoughan Avatar answered Sep 21 '22 10:09

bdoughan