Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you always need an ObjectFactory class when using JAXB?

Tags:

Do you always need an ObjectFactory class when using JAXB?

Without it I get this exception:

javax.xml.bind.JAXBException: "com.a.b.c" doesnt contain ObjectFactory.class or jaxb.index

I gather the ObjectFactory can be overkill. But given this exception I'm guessing you need it.. but not sure why?

like image 229
Marcus Leon Avatar asked Apr 19 '10 13:04

Marcus Leon


People also ask

What is the use of ObjectFactory in jaxb?

jaxb package. An ObjectFactory allows you to programatically construct new instances of the Java representation for XML content. The Java representation of XML content can consist of schema derived interfaces and classes representing the binding of schema type definitions, element declarations and model groups.

What is ObjectFactory class?

The ObjectFactory class is part of the primer. po package. ObjectFactory provides factory methods for instantiating Java interfaces representing XML content in the Java content tree. Method names are generated by concatenating: The string constant create.


2 Answers

You get that exception when you use the JAXBContext.newInstance(String) factory method, where you pass in the package name as the argument. This does require the ObjectFactory to be there, otherwise, JAXB doesn't know which classes to process.

If you don't have an ObjectFactory, you need to JAXBContext.newInstance(Class...) instead, passing in the explicit list of annotated classes to add to the context.

like image 120
skaffman Avatar answered Sep 21 '22 12:09

skaffman


Instead of the ObjectFactory you can include a jaxb.index file which is a text file that contains a new line seperated list of Java classes.

For an example of using a jaxb.index file see:

  • http://bdoughan.blogspot.com/2010/08/using-xmlanyelement-to-build-generic.html
like image 44
bdoughan Avatar answered Sep 22 '22 12:09

bdoughan