package com.jt.dto;
import org.apache.commons.lang3.StringUtils;
import java.util.HashMap;
import java.util.Map;
/**
* User: ldj
* Date: 2022/2/26
* Time: 20:30
* Description: No Description
*/
public class SpellXML {
public static void main(String[] args) {
//模拟接口参数,别人传过来的
Map<String, Object> request = new HashMap();
request.put("Name2", "莉莉");
request.put("Gender2", "女");
request.put("Gender1", "男");
StringBuilder builder = new StringBuilder();
builder.append("<Message>");
builder.append("\r\n");
builder.append("<School>");
builder.append("\r\n");
//自己定义的 插入顺序不确定
Map<String, String> map1 = new HashMap<>();
map1.put("SchoolName", "草帽中学");
map1.put("SchoolId", "3333");
map1.put("SchoolAge", "100");
map1.forEach((k, v) -> {
builder.append(String.format("<%s>%s</%s>", k, v, k));
builder.append("\r\n");
});
builder.append("</School>");
builder.append("\r\n");
builder.append("<Class>");
builder.append("\r\n");
//自己定义的 按插入顺序遍历
Map<String, String> map2 = new LinkedHashMap<>();
map2.put("ClassName1", "高三7班");
map2.put("ClassName2", "高三9班");
map2.put("ClassName3", "高三3班");
map2.forEach((k, v) -> {
builder.append(String.format("<%s>%s</%s>", k, v, k));
builder.append("\r\n");
});
String name1 = (String) request.get("Name1");
String gender1 = (String) request.get("Gender1");
String name2 = (String) request.get("Name2");
String gender2 = (String) request.get("Gender2");
if (request.containsKey("Name1") || request.containsKey("Gender1")) {
builder.append("<Teacher>");
builder.append("\r\n");
if (StringUtils.isNotBlank(name1)) {
builder.append("<Name1>" + name1 + "</Name1>");
builder.append("\r\n");
}
if (StringUtils.isNotBlank(gender1)) {
builder.append("<Gender1>" + gender1 + "</Gender1>");
builder.append("\r\n");
}
builder.append("</Teacher>");
builder.append("\r\n");
}
if (request.containsKey("Name2") || request.containsKey("Gender2")) {
builder.append("<Student>");
builder.append("\r\n");
if (StringUtils.isNotBlank(name2)) {
builder.append("<Name2>" + name2 + "</Name2>");
builder.append("\r\n");
}
if (StringUtils.isNotBlank(gender2)) {
builder.append("<Gender2>" + gender2 + "</Gender2>");
builder.append("\r\n");
}
builder.append("</Student>");
builder.append("\r\n");
}
builder.append("</Class>");
builder.append("\r\n");
builder.append("</Message>");
System.out.println(builder.toString());
}
}
?
|