Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;

import javax.xml.namespace.QName;

public class Main {
    public static void parseBeanToXmlFileByJAXB(String path, Object bean, Class clase) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(clase);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        JAXBElement<Object> rootElement = new JAXBElement<Object>(new QName(clase.getSimpleName()), clase, bean);
        marshaller.marshal(rootElement, new FileOutputStream(path));
    }
}