此列举两种方式:
第一种:
createXmlByMap(Map<String, Object> map, String parentName)(推荐应用)
第二种:
为字符串拼装mapToXML(Map<?, ?> map, StringBuffer sb)
/** * 将Map转换为XML,Map可以多层转 * @param params 需要转换的map。 * @param parentName 就是map的根key,如果map没有根key,就输入转换后的xml根节点。 * @return String-->XML */ @SuppressWarnings("unchecked") public static String createXmlByMap(Map<String, Object> map, String parentName) { //获取map的key对应的value Map<String, Object> rootMap=(Map<String, Object>)map.get(parentName); if (rootMap==null) { rootMap=map; } Document doc = DocumentHelper.createDocument(); //设置根节点 doc.addElement(parentName); String xml = iteratorXml(doc.getRootElement(), parentName, rootMap); return formatXML(xml); } /** * 循环遍历params创建xml节点 * @param element 根节点 * @param parentName 子节点名字 * @param params map数据 * @return String-->Xml */ @SuppressWarnings("unchecked") public static String iteratorXml(Element element, String parentName, Map<String, Object> params) { Element e = element.addElement(parentName); Set<String> set = params.keySet(); for (Iterator<String> it = set.iterator(); it.hasNext();) { String key = (String) it.next(); if (params.get(key) instanceof Map) { iteratorXml(e, key, (Map<String, Object>) params.get(key)); } else if (params.get(key) instanceof List) { List<Object> list = (ArrayList<Object>) params.get(key); for (int i = 0; i < list.size(); i++) { if (list.get(i) instanceof Map) { //判断是否最底层的list iteratorXml(e, key, (Map<String, Object>) list.get(i)); }else { e.addElement(key).addText((String) list.get(i)); } } } else { String value = params.get(key) == null ? "" : params.get(key).toString(); e.addElement(key).addText(value); // e.addElement(key).addCDATA(value); } } return e.asXML(); } /** * 格式化xml * @param xml * @return */ public static String formatXML(String xml) { String requestXML = null; XMLWriter writer = null; Document document = null; try { SAXReader reader = new SAXReader(); document = reader.read(new StringReader(xml)); if (document != null) { StringWriter stringWriter = new StringWriter(); OutputFormat format = new OutputFormat(" ", true);// 格式化,每一级前的空格 format.setEncoding("utf-8"); //后添加编码格式 format.setNewLineAfterDeclaration(false); // xml声明与内容是否添加空行 format.setSuppressDeclaration(false); // 是否设置xml声明头部 false:添加 format.setNewlines(true); // 设置分行 writer = new XMLWriter(stringWriter, format); writer.write(document); writer.flush(); requestXML = stringWriter.getBuffer().toString(); } return requestXML; } catch (Exception e1) { e1.printStackTrace(); return null; } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { } } } } /** * 第二种 * 手动组装map转为string * @param map * @return */ public static String moveMapToXML(Map<?, ?> map) { StringBuffer sb = new StringBuffer(); mapToXML(map, sb); return formatXML(sb.toString()); } /** * * @param map 循环遍历map节点和value * @param sb 输出xml */ private static void mapToXML(Map<?, ?> map, StringBuffer sb) { Set<?> set = map.keySet(); for (Iterator<?> it = set.iterator(); it.hasNext();) { String key = (String) it.next(); Object value = map.get(key); if (value instanceof HashMap) { sb.append("<" + key + ">"); mapToXML((HashMap<?, ?>) value, sb); sb.append("</" + key + ">"); } else if (value instanceof ArrayList) { List<?> list = (ArrayList<?>) map.get(key); for (int i = 0; i < list.size(); i++) { sb.append("<" + key + ">"); Map<?, ?> hm = (HashMap<?, ?>) list.get(i); mapToXML(hm, sb); sb.append("</" + key + ">"); } } else { sb.append("<" + key + ">" + value + "</" + key + ">"); } } }
本文由一叶发布,不代表一叶立场,转载联系作者并注明出处:https://yiyeo.com/java/35.html