Java Utililty Methods XML JAXB Serialize

List of utility methods to do XML JAXB Serialize

Description

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

Method

voidserialize(JAXBElement emp, Class clazz, OutputStream out)
serialize
JAXBContext context = getContext(clazz);
try {
    Marshaller m = context.createMarshaller();
    if (debug) {
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 
        m.marshal(emp, System.out);
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
    m.marshal(emp, out);
} catch (JAXBException e) {
    throw new IllegalStateException(e);
Stringserialize(JAXBElement object)
serialize
Class<?> clazz = object.getValue().getClass();
JAXBContext context = JAXBContext.newInstance(clazz.getPackage().getName());
Marshaller marshaller = context.createMarshaller();
StringWriter sw = new StringWriter();
marshaller.marshal(object, sw);
sw.close();
return sw.toString();
voidserialize(Object o, OutputStream os, Boolean format)
serialize
Marshaller m = CTX.createMarshaller();
m.setEventHandler(new DefaultValidationEventHandler());
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format);
m.marshal(o, os);
voidserialize(Object o, OutputStream os, Boolean format)
serialize
String pkg = o.getClass().getPackage().getName();
JAXBContext jc = getCachedContext(pkg);
Marshaller m = jc.createMarshaller();
m.setEventHandler(new DefaultValidationEventHandler());
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format);
m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
m.marshal(o, os);
Stringserialize(Object obj)
serialize
try {
    JAXBContext jc = JAXBContext.newInstance(obj.getClass());
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    StringWriter sw = new StringWriter();
    m.marshal(obj, sw);
    return sw.toString();
...
byte[]serialize(Object obj)
serialize
ByteArrayOutputStream bos = new ByteArrayOutputStream();
serialize(obj, bos);
return bos.toByteArray();
voidserialize(Object object, Class clazz, String filename)
serialize
JAXBContext context = JAXBContext.newInstance(clazz);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(object, new File(filename));
voidserialize(T object, Class objectClass, OutputStream resultStream)
serialize
serialize(object, resultStream);
voidserialize(T object, Path path)
serialize
serialize(object, path.toFile());
voidserializeFile(String path, Object o)
Serializes an object and saves it to a file, located at given path.
try {
    JAXBContext context = JAXBContext.newInstance(o.getClass());
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    FileOutputStream stream = new FileOutputStream(path);
    m.marshal(o, stream);
} catch (JAXBException e) {
    e.printStackTrace();
...