JAVA实现map集合转Xml格式

 

 

import java.util.Iterator;
import java.util.SortedMap;
import java.util.TreeMap;

public class MainTest {

    /**
     * map转xml格式
     * @param sortedMap
     * @return
     */
    public static String mapToXml(SortedMap<Object, Object> sortedMap) {
        StringBuffer sb = new StringBuffer("<Request>");
        Iterator iterator = sortedMap.keySet().iterator();
        while (iterator.hasNext()) {
            Object key = (String) iterator.next();
            Object value = sortedMap.get(key);
            sb.append("<" + key + ">");
            sb.append(value);
            sb.append("</" + key + ">");
        }
        sb.append("</Request>");
        return sb.toString();
    }


    public static void main(String[] args) {
        SortedMap<Object, Object> paramMap = new TreeMap<Object, Object>();
        paramMap.put("code1", 111);
        paramMap.put("code2", 222);
        paramMap.put("code3", 333);
        String soapXML =mapToXml(paramMap);
        System.out.println(soapXML);
    }
}

 

TreeMap是按照自然排序顺序的(可以理解字符串a,b,c的顺序),并不是按照你元素插入的顺序排序的,

如果需要按照你插入的顺序排序那么就使用:LinkedHashMap

 

 

 

输出

<Request><code1>111</code1><code2>222</code2><code3>333</code3></Request>

 

如果是多结构的话,可以使用下方这个

/**
     * map转xml格式(多结构拼接生成)
     * @param sortedMap
     * @return
     */
    public static String mapToSignXml(SortedMap<Object, Object> sortedMap) {
        StringBuilder builder = new StringBuilder();
        Iterator iterator = sortedMap.keySet().iterator();
        while (iterator.hasNext()) {

            Object key = (String) iterator.next();
            Object value = sortedMap.get(key);
            builder.append("<" + key + ">");
            if (value instanceof SortedMap) {
                SortedMap sortedMap1 = (SortedMap) value;
                builder.append(mapToSignXml(sortedMap1));
            } else {
                builder.append(value);
            }
            builder.append("</" + key + ">");

        }
        return builder.toString();
    }

    public static void main(String[] args) {

        SortedMap<Object, Object> requestMap = new TreeMap<Object, Object>();

        SortedMap<Object, Object> paramMap = new TreeMap<Object, Object>();
        paramMap.put("requid", "111");

        SortedMap<Object, Object> retrieveargsMap = new TreeMap<Object, Object>();

        retrieveargsMap.put("code1", 111);
        retrieveargsMap.put("code2", 222);

        SortedMap<Object, Object> paramMap1 = new TreeMap<Object, Object>();
        paramMap1.put("child1", "1111");
        paramMap1.put("child2", "2222");
        paramMap1.put("child3", "3333");
        retrieveargsMap.put("code3", paramMap1);

        paramMap.put("retriev", retrieveargsMap);

        requestMap.put("request",paramMap);

        String soapXML = mapToSignXml(requestMap);
        System.out.println(soapXML);

    }

 

生成结果

<request>
    <requid>111</requid>
    <retriev>
        <code1>111</code1>
        <code2>222</code2>
        <code3>
            <child1>1111</child1>
            <child2>2222</child2>
            <child3>3333</child3>
        </code3>
    </retriev>
</request>

 

JAVA实现XML格式数据转JsonObject:https://www.cnblogs.com/pxblog/p/17137487.html

 

posted @ 2020-11-19 15:46  yvioo  阅读(5451)  评论(0编辑  收藏  举报