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

JAXBContextgetJAXBContext(String namespaces)
get JAXB Context
jaxbContext = JAXBContext.newInstance("com.sun.xml.wss.saml.internal.saml20.jaxb20" + ":" + namespaces);
return jaxbContext;
JAXBContextgetJAXBContext(String packagePrefix)
get JAXB Context
return JAXBContext.newInstance(packagePrefix);
JAXBContextgetJAXBContextForWebService(Class webService)
get JAXB Context For Web Service
if (!hasWebServiceAnnotation(webService)) {
    throw new IllegalArgumentException("no webservice class");
Set<Class<?>> classes = new HashSet<>();
collect(webService, classes);
try {
    Class[] classesToBeBound = classes.stream()
            .filter(clazz -> clazz.getDeclaredAnnotation(XmlRootElement.class) != null)
...
JAXBContextgetRequestContext()
get Request Context
try {
    if (jaxbRequestContext == null)
        jaxbRequestContext = JAXBContext.newInstance("flowers.salta.resource.request");
} catch (JAXBException je) {
    je.printStackTrace();
return jaxbRequestContext;
JAXBContextgetRightsContext()
get Rights Context
return JAXBContext.newInstance("eu.prestoprime.model.ext.rights");
booleanisElement(String contextPath, Object object)
is Element
try {
    return object != null && getJAXBContext(contextPath).createJAXBIntrospector().isElement(object);
} catch (JAXBException ex) {
    throw new RuntimeException(ex);
TparseXMLFile(String fileName, String contextPath)
Parses the given XML file.
FileReader fileReader = new FileReader(fileName);
JAXBContext jc = JAXBContext.newInstance(contextPath);
Unmarshaller u = jc.createUnmarshaller();
@SuppressWarnings("unchecked")
T xRoot = ((JAXBElement<T>) u.unmarshal(fileReader)).getValue();
return xRoot;
StringtoString(JAXBContext context, Object object)
to String
final Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
final StringWriter sw = new StringWriter();
marshaller.marshal(object, sw);
return sw.toString();
StringtoXML(JAXBContext context, T obj)
Converts this response object to its XML representation unsing JAXB.
StringWriter sw = new StringWriter();
Marshaller m = context.createMarshaller();
m.setProperty(JAXB_FORMATTED_OUTPUT, true);
m.marshal(obj, sw);
return sw.toString();