Java XML JAXB Object to XML write(String filepath, T content, Class typeParameterClass)

Here you can find the source of write(String filepath, T content, Class typeParameterClass)

Description

Creates a new XML file and writes the content of the specified object into the XML file.

License

Open Source License

Parameter

Parameter Description
filepath Path to the XML file.
content Content as an object of the specified class.
typeParameterClass Class of the object with the content.

Return

File.

Declaration

public static <T> File write(String filepath, T content, Class<T> typeParameterClass) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.io.UnsupportedEncodingException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Main {
    /**//from  w  ww  . ja  v  a  2  s . c om
     * Creates a new XML file and writes the content of the specified object into the XML file. 
     * @param filepath            Path to the XML file.
     * @param content            Content as an object of the specified class.
     * @param typeParameterClass   Class of the object with the content.
     * @return                  File.
     */
    public static <T> File write(String filepath, T content, Class<T> typeParameterClass) {
        File file = null;
        try {
            file = new File(filepath);
            if (!file.exists()) {
                file.createNewFile();
            }
            file = write(file, content, typeParameterClass);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    /**
     * Writes the content of the specified object into the specified XML file.
     * @param filename            Path to the XML file.
     * @param content            Content as an object of the specified class.
     * @param typeParameterClass   Class of the object with the content.
     * @return                  File.
     */
    public static <T> File write(File file, T content, Class<T> typeParameterClass) {
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            // output pretty printed
            //jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            jaxbMarshaller.marshal(content, file);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static <T> String write(T content, Class<T> typeParameterClass) {
        ByteArrayOutputStream baos = null;
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            // output pretty printed
            //jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            baos = new ByteArrayOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(baos, "UTF-8");
            jaxbMarshaller.marshal(content, osw);
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return baos != null ? baos.toString() : null;
    }
}

Related

  1. toXml(T object)
  2. toXMLString(final T binding)
  3. toXMLString(Object obj)
  4. toXmlString(T obj, Class type)
  5. write(Object jaxbAnnotatedObj, OutputStream os)
  6. writeJaxbObject(OutputStream outputStream, JAXBElement jaxbElement, Class modelClass)
  7. xmlEntityToString(final T entity)
  8. XmlObjectToString(Object o)