Here you can find the source of map2Xml(Map map)
public static String map2Xml(Map map)
//package com.java2s; //License from project: Apache License import java.io.StringReader; import java.io.StringWriter; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.xml.sax.InputSource; public class Main { public static String map2Xml(Map map) { StringBuffer sbf = new StringBuffer(); map2Xml(sbf, map);/* w w w .ja v a 2 s . c o m*/ return xmlFormat(sbf.toString()); } private static void map2Xml(StringBuffer buffer, Map map) { Set set = map.entrySet(); Iterator iterator = set.iterator(); StringBuffer childSbf = new StringBuffer(); while (iterator.hasNext()) { Entry e = (Entry) iterator.next(); String key = (String) e.getKey(); Object value = e.getValue(); if (value instanceof List) { List list = (List) value; for (int j = 0; j < list.size(); j++) { childSbf.append("<").append(key).append(">"); Map valueMap = (Map) list.get(j); map2Xml(childSbf, valueMap); childSbf.append("</").append(key).append(">"); } } else if (value instanceof Map) { if (key != null) { childSbf.append("<").append(key).append(">"); } Map valueMap = (Map) value; map2Xml(childSbf, valueMap); if (key != null) { childSbf.append("</").append(key).append(">"); } } else { if (key != null) { childSbf.append("<").append(key).append(">"); } if (null != value) { childSbf.append(value); } else { childSbf.append(""); } if (key != null) { childSbf.append("</").append(key).append(">"); } } } buffer.append(childSbf); } private static String xmlFormat(String xml) { StringWriter writer; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db; db = dbf.newDocumentBuilder(); Document doc = db.parse(new InputSource(new StringReader(xml))); DOMSource source = new DOMSource(doc); writer = new StringWriter(); Result result = new StreamResult(writer); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(source, result); return (writer.getBuffer().toString()); } catch (Exception e) { e.printStackTrace(); } return null; } }