JAXB

To be able to create objects from XML elements, the unmarshaller must have an object factory with methods for creating all sorts of objects. Therefore, each package containing JAXB classes must contain one class ObjectFactory, annotated with XmlRegistry.

@XmlRegistry
public class ObjectFactory {
  ...
}

Most objects require nothing but a simple create method. But whenever an element has to be represented as a JAXBElement<?>, an additional factory method for wrapping the "pure" Java object of some class Foo into an element of class JAXBElement<Foo> must be provided. This method is then annotated with XmlElementDecl, providing the components of the element's tag name through the attributes namespace and name. This is a snippet from some object factory where an element of TreeType is wrapped into a JAXBElement<TreeType>:

@XmlElementDecl(namespace = "", name = "tree")
public JAXBElement<TreeType> createTree( TreeType value) {
    return new JAXBElement<TreeType>(_Tree_QNAME, TreeType.class, null, value);
}
原文地址:https://www.cnblogs.com/huey/p/5511735.html