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