map转实体类,下划线隔开转驼峰式命名;map转xml;xml转map;转换
map转实体类,下划线隔开转驼峰式命名;
package com.eoma.autocoding.utils; import com.eoma.autocoding.common.Table; import java.lang.reflect.Field; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Set; public class MapToObject { public static void main(String[] args) { HashMap<String, Object> map = new HashMap<>(); map.put("name","1"); map.put("dbName","2"); map.put("table_desc","3"); Table detail = new Table(); System.out.println(mapToObj(map,detail)); } /** * 只支持下划线隔开、驼峰式属性名(用之前请测试) * @param map * @param t * @param <T> * @return */ public static <T> T mapToObj(Map<String ,?> map, T t){ Set<String> keySet = map.keySet(); keySet.forEach(s->{ Object value = map.get(s); String name = toCamelCase(s); Field[] declaredFields = t.getClass().getDeclaredFields(); Arrays.stream(declaredFields).filter(d->d.getName().equalsIgnoreCase(name)).forEach(field ->{ try { field.setAccessible(true); field.set(t,value); } catch (Exception e) { e.printStackTrace(); } }); }); return t; } /** * 下划线隔开转驼峰式命名 * @param str * @return */ public static String toCamelCase(String str) { if (str == null || "".equals(str.trim())) { return null; } else { str = str.toLowerCase(); StringBuilder sb = new StringBuilder(str.length()); boolean upperCase = false; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c == '_') { if( i == 0 ){ sb.append(c); }else{ upperCase = true; } } else if (upperCase) { sb.append(Character.toUpperCase(c)); upperCase = false; } else { sb.append(c); } } return sb.toString(); } } }
map转xml;
public static String mapToXml(HashMap<String,Object> paraMap){ StringBuffer sb = new StringBuffer(); sb.append("<xml>"); Set es = paraMap.entrySet(); Iterator it = es.iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); String k = (String) entry.getKey(); Object v = entry.getValue(); if (null != v && !"key".equals(k)) { sb.append("<"+k+">" + v +"</"+k+">"); } } sb = sb.append("</xml>"); return sb.toString(); }
xml转map;
/** * XML格式字符串转换为Map * * @param strXML XML字符串 * @return XML数据转换后的Map * @throws Exception */ public static Map<String, String> xmlToMap(String strXML) throws Exception { try { Map<String, String> data = new HashMap<String, String>(); DocumentBuilder documentBuilder =newDocumentBuilder(); InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8")); org.w3c.dom.Document doc = documentBuilder.parse(stream); doc.getDocumentElement().normalize(); NodeList nodeList = doc.getDocumentElement().getChildNodes(); for (int idx = 0; idx < nodeList.getLength(); ++idx) { Node node = nodeList.item(idx); if (node.getNodeType() == Node.ELEMENT_NODE) { org.w3c.dom.Element element = (org.w3c.dom.Element) node; data.put(element.getNodeName(), element.getTextContent()); } } try { stream.close(); } catch (Exception ex) { // do nothing } return data; } catch (Exception ex) { throw ex; } } public static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); documentBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false); documentBuilderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); documentBuilderFactory.setXIncludeAware(false); documentBuilderFactory.setExpandEntityReferences(false); return documentBuilderFactory.newDocumentBuilder(); }
标签:
工具类
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2018-05-31 java导出数据EXCEL的工具类
2018-05-31 mysql语法