package Test;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class demo10 {
public static void main(String[] args) {
TreeMap requestHeaderMap = new TreeMap();
TreeMap controlMap = new TreeMap();
TreeMap map = new TreeMap();
TreeMap mapHead = new TreeMap();
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
.format(new Date());
map.put("_TransactionId", "_TransactionId");
requestHeaderMap.put("version", "1.0");
map.put("requestHeader", requestHeaderMap);
controlMap.put("requesterName", "MGMT");
map.put("control", controlMap);
mapHead.put("Head", map);
Document document = DocumentHelper.createDocument();
document.setXMLEncoding("UTF-8");
Element bodyElem = document.addElement("Message");
parseToXML(mapHead,bodyElem);
String xml = document.asXML();
System.out.println(xml);
}
private static void parseToXML(TreeMap map, Element bodyElem) {
Iterator records = map.entrySet().iterator();
while (records.hasNext()) {
Map.Entry entry = (Map.Entry) records.next();
if (entry.getValue().getClass().equals(TreeMap.class)) {// 子目录
bodyElem = bodyElem.addElement((String) entry.getKey());
parseToXML((TreeMap) entry.getValue(), bodyElem);
} else {
bodyElem.addElement((String) entry.getKey()).addText(
(String) entry.getValue());
}
}
}
}
【解析Map为XML】转载于:https://www.cnblogs.com/wzh0125/p/4462778.html
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- jvm|【JVM】JVM08(java内存模型解析[JMM])
- 技术|为参加2021年蓝桥杯Java软件开发大学B组细心整理常见基础知识、搜索和常用算法解析例题(持续更新...)