Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

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

public class Main {
    public static void saveXmlToFile(Object model, String filename) throws FileNotFoundException, JAXBException {
        FileOutputStream fos = new FileOutputStream(filename);
        marshal(model, fos);
    }

    public static void saveXmlToFile(Object model, File file) throws FileNotFoundException, JAXBException {
        FileOutputStream fos = new FileOutputStream(file);
        marshal(model, fos);
    }

    public static void marshal(Object model, OutputStream output) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(model.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        jaxbMarshaller.marshal(model, output);
    }
}