Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.FileOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Main {
    /**
     * Marshal a JAXB element to a XML file
     *
     * @param jaxbElement
     *            The root of content tree to be marshalled
     * @param strXMLFilePath
     *            XML output file path
     * @throws Exception
     *             in error case
     */
    public static void doMarshalling(Object jaxbElement, String strXMLFilePath) throws Exception {
        if (jaxbElement == null) {
            throw new RuntimeException("No JAXB element to marshal (null)!");
        }
        if (strXMLFilePath == null) {
            throw new RuntimeException("No XML file path (null)!");
        }
        FileOutputStream fos = null;
        try {
            JAXBContext jaxbCtx = JAXBContext.newInstance(jaxbElement.getClass().getPackage().getName());
            Marshaller marshaller = jaxbCtx.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // NOI18N
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            fos = new FileOutputStream(strXMLFilePath);
            marshaller.marshal(jaxbElement, fos);// System.out);
        } catch (Exception e) {
            //            Logger.XMLEval.logState("Marshalling failed: " + e.getMessage(), LogLevel.Error);
            throw e;
        } finally {
            if (fos != null) {
                fos.close();
                fos = null;
            }
        }
    }
}