/*
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
*/
|
package com.megatim.apifdxweb.tools.jaxb.util;
|
|
import java.io.BufferedReader;
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.InputStream;
|
import java.io.InputStreamReader;
|
import java.io.Reader;
|
import java.nio.charset.Charset;
|
import javax.xml.bind.JAXBContext;
|
import javax.xml.bind.JAXBException;
|
import javax.xml.bind.Marshaller;
|
import javax.xml.bind.Unmarshaller;
|
|
/**
|
*
|
* @author ASUS
|
*/
|
public class JaxBUtil {
|
|
public static <T> T convertXmlFileToObject(String fileName, Class<T> clazz) throws Exception {
|
JAXBContext context = JAXBContext.newInstance(clazz);
|
Unmarshaller unmarshaller = context.createUnmarshaller();
|
InputStream inputStream = new FileInputStream(fileName);
|
Reader reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
|
BufferedReader buffer = new BufferedReader(reader);
|
return (T) unmarshaller.unmarshal(buffer);
|
}
|
|
public static void buildXmlFile(Class clazz, String fileName, Object valueToWrite) throws JAXBException {
|
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
|
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
|
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
jaxbMarshaller.marshal(valueToWrite, new File(fileName));
|
//jaxbMarshaller.marshal(valueToWrite, System.out);
|
}
|
}
|