Jaxb: How do I generate ObjectFactory class?

前端 未结 3 1778
无人及你
无人及你 2021-01-05 09:42

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,

3条回答
  •  孤城傲影
    2021-01-05 10:00

    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 webLeadsElement = new JAXBElement(
        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 {
       ...
    }
    

提交回复
热议问题