import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.*;
import java.nio.charset.StandardCharsets;
/**
* @author ParanoidCAT
*/
public class JaxbUtils {
private JaxbUtils() throws Exception {
throw new Exception("no JaxbUtils instance should be created");
}
/**
* 将Java对象序列化为Xml并通过输出流输出
*
* @param object Java对象
* @param outputStream 输出流
*/
public static void javaToXml(Object object, OutputStream outputStream) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
marshaller.marshal(object, outputStream);
outputStream.flush();
} catch (JAXBException | IOException e) {
throw new RuntimeException(e);
}
}
/**
* 将Java对象序列化为Xml并输出到指定文件中
*
* @param object Java对象
* @param file 指定文件
*/
public static void javaToXml(Object object, File file) {
try {
try (OutputStream outputStream = new FileOutputStream(file)) {
javaToXml(object, outputStream);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 将Java对象序列化为Xml字符串
*
* @param object Java对象
* @return Xml字符串
*/
public static String javaToXml(Object object) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
try (StringWriter stringWriter = new StringWriter()) {
marshaller.marshal(object, stringWriter);
return stringWriter.toString();
}
} catch (JAXBException | IOException e) {
throw new RuntimeException(e);
}
}
/**
* 将Xml通过输入流输入并反序列化为Java对象实例
*
* @param tClass Java对象类型
* @param inputStream 输入流
* @param <T> Java对象
* @return Java对象实例
*/
@SuppressWarnings("unchecked")
public static <T> T xmlToJava(Class<T> tClass, InputStream inputStream) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(tClass);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
return (T) unmarshaller.unmarshal(inputStream);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* 将指定文件反序列化为Java对象实例
*
* @param tClass Java对象类型
* @param file 指定文件
* @param <T> Java对象
* @return Java对象实例
*/
public static <T> T xmlToJava(Class<T> tClass, File file) {
try {
try (InputStream inputStream = new FileInputStream(file)) {
return xmlToJava(tClass, inputStream);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 将Xml字符串反序列化为Java对象实例
*
* @param tClass Java对象类型
* @param xml Xml字符串
* @param <T> Java对象
* @return Java对象实例
*/
@SuppressWarnings("unchecked")
public static <T> T xmlToJava(Class<T> tClass, String xml) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(tClass);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
try (Reader reader = new StringReader(xml)) {
return (T) unmarshaller.unmarshal(reader);
}
} catch (JAXBException | IOException e) {
throw new RuntimeException(e);
}
}
}