Java Utililty Methods XML JAXB Context

List of utility methods to do XML JAXB Context

Description

The list of methods to do XML JAXB Context are organized into topic(s).

Method

voidclearContextCache()
clear Context Cache
for (SoftReference<JAXBContext> i : contextCache.values())
    i.clear();
contextCache.clear();
JAXBContextcreateContext(final Class bind)
Returns a cached JAXB Context.
JAXBContext context = null;
SoftReference<JAXBContext> ref = contextCache.get(bind);
if (ref != null) {
    context = ref.get();
    if (context == null)
        contextCache.remove(bind);
if (context != null)
...
JAXBContextcreateJaxbContextFor(Object obj, Class[] classes)
create Jaxb Context For
Set<Class<?>> classSet = new HashSet<>();
classSet.addAll(Arrays.asList(classes));
classSet.add(obj.getClass());
return JAXBContext.newInstance(classSet.toArray(new Class<?>[0]));
JAXBContextcreateRIContext(Class clss[], String defaultNS)
create RI Context
try {
    Class<?> cls;
    Map<String, Object> map = new HashMap<String, Object>();
    try {
        cls = Class.forName("com.sun.xml.bind.v2.ContextFactory");
        if (defaultNS != null) {
            map.put("com.sun.xml.bind.defaultNamespaceRemap", defaultNS);
    } catch (ClassNotFoundException e) {
        cls = Class.forName("com.sun.xml.internal.bind.v2.ContextFactory");
        if (defaultNS != null) {
            map.put("com.sun.xml.internal.bind.defaultNamespaceRemap", defaultNS);
    Method meth = cls.getMethod("createContext", clss.getClass(), Map.class);
    return (JAXBContext) meth.invoke(null, clss, map);
} catch (Exception e) {
    throw new JAXBException(e);
TfromXML(JAXBContext context, String requestXML)
Unmarshalls the specified XML document to a call request object.
StringReader sr = new StringReader(requestXML);
T result = null;
Unmarshaller um = context.createUnmarshaller();
result = (T) um.unmarshal(sr);
return result;
FilegenerateTemporarySchemaFile(JAXBContext context)
generate Temporary Schema File
final File tempFile = File.createTempFile("jaxb-junit-report-schema", ".xsd");
context.generateSchema(new SchemaOutputResolver() {
    @Override
    public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
        StreamResult result = new StreamResult(tempFile);
        result.setSystemId(tempFile.toURI().toURL().toString());
        return result;
});
return tempFile;
JAXBContextgetCachedContext(String pkg)
get Cached Context
JAXBContext ctx = _ctxMap.get(pkg);
if (ctx == null) {
    ctx = JAXBContext.newInstance(pkg);
    _ctxMap.put(pkg, ctx);
return ctx;
JAXBContextgetContext()
get Context
if (context == null) {
    context = JAXBContext.newInstance("org.opentestsystem.rdw.common.model.trt");
return context;
JAXBContextgetContext()
Returns the JAXBContext for this format.
if (JAXB == null)
    initContext(Thread.currentThread().getContextClassLoader());
return JAXB;
JAXBContextgetContext()
get Context
if (JAXB_CONTEXT == null) {
    throw new IllegalStateException("JAXB has not been initialized");
return JAXB_CONTEXT;