public class JaxbUtil {

     /**
* java对象转换为xml文件
*
* @param xmlPath xml文件路径
* @param load java对象.Class
* @return xml文件的String
* @throws JAXBException
*/
public static String beanToXml(Object obj, Class<?> load) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(load);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
return writer.toString();
} /**
* xml文件配置转换为对象
*
* @param xmlPath xml文件路径
* @param load java对象.Class
* @return java对象
* @throws JAXBException
* @throws IOException
*/
@SuppressWarnings("unchecked")
public static <T> T xmlToBean(String xmlPath, Class<T> load) throws JAXBException, IOException {
JAXBContext context = JAXBContext.newInstance(load);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (T) unmarshaller.unmarshal(new StringReader(xmlPath));
} /**
* JavaBean转换成xml 默认编码UTF-8
*
* @param obj
* @param writer
* @return
*/
public static String convertToXml(Object obj) {
return convertToXml(obj, "UTF-8");
} /**
* JavaBean转换成xml
*
* @param obj
* @param encoding
* @return
*/
public static String convertToXml(Object obj, String encoding) {
String result = null;
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
//去掉生成xml的默认报文头
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
StringWriter writer = new StringWriter();
writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n ");
marshaller.marshal(obj, writer);
result = writer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
} /**
* JavaBean转换成xml去除xml声明部分
*
* @param obj
* @param encoding
* @return
*/
public static String convertToXmlIgnoreXmlHead(Object obj, String encoding) {
String result = null;
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
result = writer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
} /**
* xml转换成JavaBean
*
* @param xml
* @param c
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T converyToJavaBean(String xml, Class<T> c) {
T t = null;
try {
JAXBContext context = JAXBContext.newInstance(c);
Unmarshaller unmarshaller = context.createUnmarshaller();
t = (T) unmarshaller.unmarshal(new StringReader(xml));
} catch (Exception e) {
e.printStackTrace();
}
return t;
} private static OutputFormat createPrettyPrint() {
OutputFormat format = new OutputFormat();
//format.setIndentSize(2);
format.setNewLineAfterDeclaration(false);
format.setNewlines(true);
format.setTrimText(false);
format.setPadText(false);
return format;
} /**
*
* @Title: formatXml
* @author:humingbo
* @date:2019年7月18日上午11:35:08
* @Description: 格式化xml方法
* @param str
* @return
* @throws Exception
*/
public static String formatXml(String str) throws Exception {
Document document = null;
document = DocumentHelper.parseText(str);
// 格式化输出格式
OutputFormat format = createPrettyPrint();
format.setEncoding("UTF-8");
StringWriter writer = new StringWriter();
// 格式化输出流
XMLWriter xmlWriter = new XMLWriter(writer, format);
// 将document写入到输出流
xmlWriter.write(document);
xmlWriter.close();
return writer.toString();
}
}

最新文章

  1. CodeVS 线段覆盖1~5
  2. 【HOW】如何限制Reporting Services报表导出功能中格式选项
  3. return和finally的执行顺序
  4. IOS 杂笔- 6(KVC-KVO)
  5. Creating Excel File in Oracle Forms
  6. Windows SDK笔记(经典--一定要看)
  7. 导致Asp.Net站点重启的10个原因
  8. iOS开发传感器相关
  9. windows下远程访问Redis,windows Redis绑定ip无效,Redis设置密码无效,Windows Redis 配置不生效,Windows Redis requirepass不生效,windows下远程访问redis的配置
  10. QT 启动shell脚本
  11. Flask Session 使用和源码分析 —— (6)
  12. Vue插槽:(2.6.0以后版本弃用slot和slot-scope,改用v-slot)
  13. python+selenium+unittest 实现自动化测试
  14. 理解ArrayList与LinkedList的区别
  15. 经典 mysql 28道题
  16. git 查询某人的提交记录
  17. css定位问题的记录
  18. Delphi中的DBGrid控件
  19. 003实现字符串反转reverse
  20. Spring Data JPA 简单查询

热门文章

  1. 来看一下Java中“-”与equeals的区别
  2. scrapy-redis 0.6.8 配置信息
  3. sqlalchemy相关操作(ORM)
  4. 查看 Python 对象的属性
  5. python脚本测试websocket接口协议
  6. Activiti6 查询由某人发起的流程请求 设置流程发起人
  7. 从零开始写Hystrix
  8. request有get,post,put,delete等方法大全
  9. 01-赵志勇机器学习-Logistics_Regression-train
  10. var定义变量的使用细节