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

voidsave(Class confClass, Object confObj, File xmlFile)
save
JAXBContext context = JAXBContext.newInstance(confClass);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(confObj, new FileWriter(xmlFile));
voidsave(File file, T obj, Class... clazz)
save
JAXBContext jc = JAXBContext.newInstance(clazz);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(obj, file);
voidsaveDataToFile(File file, T data)
Saves the data in the file in xml format.
assert file != null;
assert data != null;
if (!file.exists()) {
    throw new FileNotFoundException("File not found : " + file.getAbsolutePath());
JAXBContext context = JAXBContext.newInstance(data.getClass());
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
...
StringsaveModel(JAXBElement model, String packageName)
save Model
JAXBContext jc;
try {
    jc = JAXBContext.newInstance(packageName);
    Marshaller m = jc.createMarshaller();
    ByteArrayOutputStream o = new ByteArrayOutputStream();
    m.marshal(model, o);
    return new String(o.toByteArray());
} catch (Exception e) {
...
voidsaveObject(Path path, T object)
Saves the object to the file.
if (path == null || object == null) {
    throw new RuntimeException("The path to file or object is null!");
try {
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(object, path.toFile());
...
voidsaveObject(T object, Class typeClass, URL path)
save Object
try {
    File file = new File(path.toURI());
    JAXBContext jaxbContext = JAXBContext.newInstance(typeClass);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(object, file);
} catch (JAXBException e) {
    e.printStackTrace();
...
voidsaveToFile(String fileName, T obj)
save object to xml file using outputStream, JAXNContext and marshaller
FileOutputStream outputStream = null;
try {
    outputStream = new FileOutputStream(fileName);
} catch (FileNotFoundException e) {
    e.printStackTrace();
try {
    JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass());
...
voidsaveXML(Object object, String fileNamePath, Boolean append)
save XML
Marshaller marshaller = createMarshaller(object);
File file = createFile(fileNamePath);
FileWriter fileWriter;
try {
    fileWriter = new FileWriter(file, append);
    if (append) {
        fileWriter.append(System.getProperty("line.separator"));
    marshaller.marshal(object, fileWriter);
} catch (IOException e) {
    e.printStackTrace();
} catch (JAXBException e) {
    e.printStackTrace();
StringserialiseObject(Object obj, Class objclass)
serialise Object
JAXBContext jaxbContext = getJAXBContext(objclass);
Marshaller m = jaxbContext.createMarshaller();
StringWriter w = new StringWriter();
m.marshal(obj, w);
String xmlString = w.toString();
return xmlString;
Stringserialize(final Object object)
serialize
OutputStream outputStream = new ByteArrayOutputStream();
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(outputStream, object);
return outputStream.toString();