CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-javax--javaee-api

Complete Java Enterprise Edition 8 specification APIs providing all standardized enterprise application development interfaces

Pending
Overview
Eval results
Files

xml-binding.mddocs/

XML Binding

JAXB API for binding Java objects to XML representations with annotation-driven marshalling and unmarshalling support.

Core JAXB Context and Operations

JAXBContext

public abstract class JAXBContext {
    public static JAXBContext newInstance(String contextPath) throws JAXBException;
    public static JAXBContext newInstance(String contextPath, ClassLoader classLoader) throws JAXBException;
    public static JAXBContext newInstance(Class... classesToBeBound) throws JAXBException;
    public static JAXBContext newInstance(Class[] classesToBeBound, Map<String, ?> properties) throws JAXBException;
    
    public abstract Marshaller createMarshaller() throws JAXBException;
    public abstract Unmarshaller createUnmarshaller() throws JAXBException;
    public abstract Validator createValidator() throws JAXBException;
    public <T> Binder<T> createBinder(Class<T> domType);
    public JAXBIntrospector createJAXBIntrospector();
    public void generateSchema(SchemaOutputResolver outputResolver) throws IOException;
}

Marshaller Interface

public interface Marshaller {
    void marshal(Object jaxbElement, Result result) throws JAXBException;
    void marshal(Object jaxbElement, OutputStream os) throws JAXBException;
    void marshal(Object jaxbElement, File output) throws JAXBException;
    void marshal(Object jaxbElement, Writer writer) throws JAXBException;
    void marshal(Object jaxbElement, ContentHandler handler) throws JAXBException;
    void marshal(Object jaxbElement, Node node) throws JAXBException;
    void marshal(Object jaxbElement, XMLStreamWriter writer) throws JAXBException;
    void marshal(Object jaxbElement, XMLEventWriter writer) throws JAXBException;
    
    Node getNode(Object contentTree) throws JAXBException;
    
    void setProperty(String name, Object value) throws PropertyException;
    Object getProperty(String name) throws PropertyException;
    
    void setEventHandler(ValidationEventHandler handler) throws JAXBException;
    ValidationEventHandler getEventHandler() throws JAXBException;
    
    void setAdapter(XmlAdapter adapter);
    void setAdapter(Class<A> type, A adapter);
    <A extends XmlAdapter> A getAdapter(Class<A> type);
    
    void setAttachmentMarshaller(AttachmentMarshaller am);
    AttachmentMarshaller getAttachmentMarshaller();
    
    void setSchema(Schema schema);
    Schema getSchema();
    
    void setListener(Marshaller.Listener listener);
    Marshaller.Listener getListener();
    
    String JAXB_ENCODING = "jaxb.encoding";
    String JAXB_FORMATTED_OUTPUT = "jaxb.formatted.output";
    String JAXB_SCHEMA_LOCATION = "jaxb.schemaLocation";
    String JAXB_NO_NAMESPACE_SCHEMA_LOCATION = "jaxb.noNamespaceSchemaLocation";
    String JAXB_FRAGMENT = "jaxb.fragment";
}

Unmarshaller Interface

public interface Unmarshaller {
    Object unmarshal(File f) throws JAXBException;
    Object unmarshal(InputStream is) throws JAXBException;
    Object unmarshal(Reader reader) throws JAXBException;
    Object unmarshal(URL url) throws JAXBException;
    Object unmarshal(InputSource source) throws JAXBException;
    Object unmarshal(Node node) throws JAXBException;
    <T> JAXBElement<T> unmarshal(Node node, Class<T> expectedType) throws JAXBException;
    Object unmarshal(Source source) throws JAXBException;
    <T> JAXBElement<T> unmarshal(Source source, Class<T> expectedType) throws JAXBException;
    Object unmarshal(XMLStreamReader reader) throws JAXBException;
    <T> JAXBElement<T> unmarshal(XMLStreamReader reader, Class<T> expectedType) throws JAXBException;
    Object unmarshal(XMLEventReader reader) throws JAXBException;
    <T> JAXBElement<T> unmarshal(XMLEventReader reader, Class<T> expectedType) throws JAXBException;
    
    UnmarshallerHandler getUnmarshallerHandler();
    
    void setValidating(boolean validating) throws JAXBException;
    boolean isValidating() throws JAXBException;
    
    void setEventHandler(ValidationEventHandler handler) throws JAXBException;
    ValidationEventHandler getEventHandler() throws JAXBException;
    
    void setProperty(String name, Object value) throws PropertyException;
    Object getProperty(String name) throws PropertyException;
    
    void setSchema(Schema schema);
    Schema getSchema();
    
    void setAdapter(XmlAdapter adapter);
    void setAdapter(Class<A> type, A adapter);
    <A extends XmlAdapter> A getAdapter(Class<A> type);
    
    void setAttachmentUnmarshaller(AttachmentUnmarshaller au);
    AttachmentUnmarshaller getAttachmentUnmarshaller();
    
    void setListener(Unmarshaller.Listener listener);
    Unmarshaller.Listener getListener();
}

JAXB Annotations

Class-Level Annotations

@Target({ElementType.TYPE, ElementType.PACKAGE})
@Retention(RetentionPolicy.RUNTIME)
public @interface XmlRootElement {
    String name() default "##default";
    String namespace() default "##default";
}

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface XmlType {
    String name() default "##default";
    String[] propOrder() default {""};
    String namespace() default "##default";
    Class factoryClass() default DEFAULT.class;
    String factoryMethod() default "";
}

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface XmlAccessorType {
    XmlAccessType value() default XmlAccessType.PUBLIC_MEMBER;
}

public enum XmlAccessType {
    PROPERTY, FIELD, PUBLIC_MEMBER, NONE
}

Field and Property Annotations

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface XmlElement {
    String name() default "##default";
    boolean nillable() default false;
    boolean required() default false;
    String namespace() default "##default";
    String defaultValue() default "\u0000";
    Class type() default DEFAULT.class;
}

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface XmlAttribute {
    String name() default "##default";
    boolean required() default false;
    String namespace() default "##default";
}

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface XmlElementRef {
    String name() default "##default";
    String namespace() default "##default";
    Class type() default DEFAULT.class;
    boolean required() default true;
}

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface XmlElementRefs {
    XmlElementRef[] value();
}

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface XmlElementWrapper {
    String name() default "##default";
    String namespace() default "##default";
    boolean nillable() default false;
    boolean required() default false;
}

Value and Content Annotations

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface XmlValue {
}

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface XmlTransient {
}

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface XmlAnyElement {
    boolean lax() default false;
    Class<? extends DomHandler> value() default W3CDomHandler.class;
}

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface XmlAnyAttribute {
}

Schema Generation and Validation

Schema Generation

public abstract class SchemaOutputResolver {
    public abstract Result createOutput(String namespaceUri, String suggestedFileName) throws IOException;
}

public interface SchemaGenerator {
    void generateSchema() throws IOException;
}

Validation Events

public interface ValidationEventHandler {
    boolean handleEvent(ValidationEvent event);
}

public interface ValidationEvent {
    int getSeverity();
    String getMessage();
    ValidationEventLocator getLocator();
    Throwable getLinkedException();
    
    int WARNING = 0;
    int ERROR = 1;
    int FATAL_ERROR = 2;
}

public interface ValidationEventLocator {
    URL getURL();
    int getOffset();
    int getLineNumber();
    int getColumnNumber();
    Object getObject();
    Node getNode();
}

XML Adapters

XmlAdapter

public abstract class XmlAdapter<ValueType, BoundType> {
    protected XmlAdapter();
    public abstract BoundType unmarshal(ValueType v) throws Exception;
    public abstract ValueType marshal(BoundType v) throws Exception;
}

@Target({ElementType.PACKAGE, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface XmlJavaTypeAdapter {
    Class<? extends XmlAdapter> value();
    Class type() default DEFAULT.class;
}

@Target({ElementType.PACKAGE})
@Retention(RetentionPolicy.RUNTIME)
public @interface XmlJavaTypeAdapters {
    XmlJavaTypeAdapter[] value();
}

Usage Examples

Basic Object Binding

@XmlRootElement
@XmlType(propOrder = {"id", "name", "email"})
public class Person {
    @XmlElement(required = true)
    private Long id;
    
    @XmlElement
    private String name;
    
    @XmlAttribute
    private String email;
    
    // getters and setters
}

// Marshal to XML
JAXBContext context = JAXBContext.newInstance(Person.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

Person person = new Person();
person.setId(1L);
person.setName("John Doe");
person.setEmail("john@example.com");

marshaller.marshal(person, System.out);

// Unmarshal from XML
Unmarshaller unmarshaller = context.createUnmarshaller();
Person person = (Person) unmarshaller.unmarshal(new File("person.xml"));

Custom Adapter Example

public class DateAdapter extends XmlAdapter<String, Date> {
    
    private static final String PATTERN = "yyyy-MM-dd";
    
    @Override
    public Date unmarshal(String dateString) throws Exception {
        return new SimpleDateFormat(PATTERN).parse(dateString);
    }
    
    @Override
    public String marshal(Date date) throws Exception {
        return new SimpleDateFormat(PATTERN).format(date);
    }
}

// Usage in class
@XmlJavaTypeAdapter(DateAdapter.class)
private Date birthDate;

Install with Tessl CLI

npx tessl i tessl/maven-javax--javaee-api

docs

dependency-injection.md

ejb.md

enterprise-services.md

index.md

json-processing.md

messaging.md

persistence.md

rest-services.md

security.md

transactions.md

validation.md

web-services.md

web-technologies.md

xml-binding.md

tile.json