/**
* @Description:
*
* @Title: JAXBUtil.java
* @Package com.joyce.util
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-10 下午02:54:40
* @version V2.0
*/
package com.joyce.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
/**
* @Description:JAXB对象和XML转换util
*
* @ClassName: JAXBUtil
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-10 下午02:54:40
* @version V2.0
*/
public class JAXBUtil {
/**
* @Description: 将对象转换为XML
*
* @param obj
* @param beanClass
* @return
* @throws Exception
*
* @Title: JAXBUtil.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-10 下午04:23:45
* @version V2.0
*/
@SuppressWarnings("unchecked")
public String objectToXmlStr(Object obj, Class beanClass) throws Exception {
JAXBContext context = JAXBContext.newInstance(beanClass);
// 根据上下文获取marshaller对象
Marshaller marshaller = context.createMarshaller();
// 设置编码字符集
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
// 格式化XML输出,有分行和缩进
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// 打印到控制台
marshaller.marshal(obj, System.out);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
marshaller.marshal(obj, baos);
String xmlObj = new String(baos.toByteArray());
return xmlObj.replace(" standalone=\"yes\"", "");
}
/**
* @Description: 将对象转换为XML并且写入文件
*
* @param obj
* @param beanClass
* @param file
* @throws Exception
*
* @Title: JAXBUtil.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-10 下午04:24:13
* @version V2.0
*/
@SuppressWarnings("unchecked")
public void objectToXmlStr(Object obj, Class beanClass, File file) throws Exception {
JAXBContext context = JAXBContext.newInstance(beanClass);
// 根据上下文获取marshaller对象
Marshaller marshaller = context.createMarshaller();
// 设置编码字符集
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
// 格式化XML输出,有分行和缩进
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// 打印到控制台
marshaller.marshal(obj, System.out);
marshaller.marshal(obj, file);
}
/**
* @Description: XML转换为对象
*
* @param <T>
* @param file
* @param beanClass
* @return
* @throws Exception
*
* @Title: JAXBUtil.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-10 下午04:24:50
* @version V2.0
*/
@SuppressWarnings("unchecked")
public <T> T xmlStrToObject(File file, Class<T> beanClass) throws Exception {
T bean = beanClass.newInstance();
JAXBContext context = JAXBContext.newInstance(beanClass);
Unmarshaller unmarshaller = context.createUnmarshaller();
bean = (T) unmarshaller.unmarshal(file);
return bean;
}
}

Joyce-Luo
- 粉丝: 1035
最新资源
- 基于PLC的电机调速控制系统-广州大学.docx
- 香港地铁工程项目管理模式和经验.pptx
- 项目七网络营销专员.pptx
- 软考系统集成项目管理工程师上午试题答案.doc
- 网络安全培训(安全意识).pptx
- 网络营销实务之整理调研资料.pptx
- 微软应用软件架构设计指南.pptx
- 基于单片机的多点温度采集与记录系统.doc
- 综合布线系统施工.ppt
- 配电网自动化技术课程设计样本.doc
- 数值分析上机(C++版).doc
- 内蒙发电厂自动化控制系统图.doc
- 项目一电动机起停的PLC控制.docx
- 软件工程导论最全复习总结.doc
- 项目管理人员责任制考核办法.doc
- 基于JAVA局域网聊天程序设计与实现附源代码.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



- 1
- 2
- 3
前往页