实现json转map map转xml
记录dom4j gson实现记录
记录参数配置
基础参数 id、name
数组参数 arr
含有属性的基础参数 po.id
属性参数和命令空间参数: -xmlns
根元素: root
实现 xml中标签属性的插入 和命令空间属性的插入
普通xml报文如下
<?xml version="1.0" encoding="UTF-8"?>
<root>
<root xmlns="sss">
<arr>
<name>ca</name>
<id>1</id>
</arr>
<arr>
<name>cs</name>
<id>2</id>
</arr>
<id>1</id>
<po>
<id xml="sasd">1</id>
<code>AD123</code>
</po>
</root>
</root>
json转map 方法
public static Map<String, Object> json2Map(String jsonStr) {
Map<String, Object> map = new HashMap<>();
ObjectMapper objectMapper = new ObjectMapper();
if(jsonStr != null && !"".equals(jsonStr)){
//最外层解析
try {
Map jsonObject = objectMapper.readValue(jsonStr, Map.class);
for (Object k : jsonObject.keySet()) {
Object v = jsonObject.get(k);
//如果内层还是数组的话,继续解析
if (v instanceof ArrayList) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Iterator<Map> it = ((ArrayList) v).iterator();
while (it.hasNext()) {
Map json2 = it.next();
list.add(json2Map(objectMapper.writeValueAsString(json2)));
}
map.put(k.toString(), list);
} else {
map.put(k.toString(), v);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return map;
}else{
return null;
}
}
map转xml
public static String jsonToXml(Object json, String rootKey) {
Document document = DocumentHelper.createDocument();
QName qName = new QName(rootKey);
Element element = document.addElement(qName);
document.setRootElement(element);
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonObject jsonObject = new JsonParser().parse(objectMapper.writeValueAsString(json)).getAsJsonObject();
toXml(jsonObject, document.getRootElement(), null);
return document.asXML();
} catch (Exception e) {
log.error(e.getMessage());
throw new AppException(e.getMessage());
}
}
private static Element toXml(JsonElement jsonElement, Element parentElement, String name) {
if (jsonElement instanceof JsonArray) {
JsonArray sonJsonArray = (JsonArray)jsonElement;
for (int i = 0; i < sonJsonArray.size(); i++) {
JsonElement arrayElement = sonJsonArray.get(i);
toXml(arrayElement, parentElement, name);
}
}else if (jsonElement instanceof JsonObject) {
JsonObject sonJsonObject = (JsonObject) jsonElement;
Element currentElement = null;
if (name != null) {
currentElement = parentElement.addElement(name);
}
Set<Map.Entry<String, JsonElement>> set = sonJsonObject.entrySet();
for (Map.Entry<String, JsonElement> s : set) {
toXml(s.getValue(), currentElement != null ? currentElement : parentElement, s.getKey());
}
}else if (jsonElement.isJsonNull()){
parentElement.addElement(name);
}else {
addAttribute(parentElement, name, jsonElement.getAsString());
}
return parentElement;
}
private static void addAttribute(Element element, String name, String value) {
if (String.valueOf(name.charAt(0)).equals(StringPool.DASH)){
String key=name.substring(1);
if (key.equals("xmlns")){
Namespace namespace=new Namespace("",value);
QName qName=new QName(element.getName(),namespace);
element.setQName(qName);
}else {
element.addAttribute(key,value);
}
}else if (StringPool.HASH.equals(String.valueOf(name.charAt(0)))){
element.addText(value);
}else {
Element el = element.addElement(name);
el.addText(value);
}
}
额外记录 mybatisplus StringPool常用符号常量
String AMPERSAND = "&";
String AND = "and";
String AT = "@";
String ASTERISK = "*";
String STAR = "*";
String BACK_SLASH = "\\";
String COLON = ":";
String COMMA = ",";
String DASH = "-";
String DOLLAR = "$";
String DOT = ".";
String DOTDOT = "..";
String DOT_CLASS = ".class";
String DOT_JAVA = ".java";
String DOT_XML = ".xml";
String EMPTY = "";
String EQUALS = "=";
String FALSE = "false";
String SLASH = "/";
String HASH = "#";
String HAT = "^";
String LEFT_BRACE = "{";
String LEFT_BRACKET = "(";
String LEFT_CHEV = "<";
String DOT_NEWLINE = ",\n";
String NEWLINE = "\n";
String N = "n";
String NO = "no";
String NULL = "null";
String OFF = "off";
String ON = "on";
String PERCENT = "%";
String PIPE = "|";
String PLUS = "+";
String QUESTION_MARK = "?";
String EXCLAMATION_MARK = "!";
String QUOTE = "\"";
String RETURN = "\r";
String TAB = "\t";
String RIGHT_BRACE = "}";
String RIGHT_BRACKET = ")";
String RIGHT_CHEV = ">";
String SEMICOLON = ";";
String SINGLE_QUOTE = "'";
String BACKTICK = "`";
String SPACE = " ";
String TILDA = "~";
String LEFT_SQ_BRACKET = "[";
String RIGHT_SQ_BRACKET = "]";
String TRUE = "true";
String UNDERSCORE = "_";
String UTF_8 = "UTF-8";
String US_ASCII = "US-ASCII";
String ISO_8859_1 = "ISO-8859-1";
String Y = "y";
String YES = "yes";
String ONE = "1";
String ZERO = "0";
String DOLLAR_LEFT_BRACE = "${";
String HASH_LEFT_BRACE = "#{";
String CRLF = "\r\n";
String HTML_NBSP = " ";
String HTML_AMP = "&";
String HTML_QUOTE = """;
String HTML_LT = "<";
String HTML_GT = ">";
String[] EMPTY_ARRAY = new String[0];
byte[] BYTES_NEW_LINE = "\n".getBytes();
|