Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Main {

    @SuppressWarnings({ "unchecked" })
    public static String map2Xml(Map srcMap) {
        StringBuffer xmlBuffer = new StringBuffer("");
        Iterator ite = srcMap.keySet().iterator();
        String key;
        Object obj;
        while (ite.hasNext()) {
            key = (String) ite.next();
            obj = srcMap.get(key);
            if (obj instanceof Map) {
                wrapLeftSign(xmlBuffer, key);
                xmlBuffer.append(map2Xml((Map) obj));
                wrapRightSign(xmlBuffer, key);
            } else if (obj instanceof List) {
                if (((List) obj).get(0) instanceof Map) {
                    for (Map map : (List<Map>) obj) {
                        wrapLeftSign(xmlBuffer, key);
                        xmlBuffer.append(map2Xml(map));
                        wrapRightSign(xmlBuffer, key);
                    }
                } else {
                    for (Object something : (List<Object>) obj) {
                        wrapLeftSign(xmlBuffer, key);
                        xmlBuffer.append(something);
                        wrapRightSign(xmlBuffer, key);
                    }
                }
            } else {
                wrapLeftSign(xmlBuffer, key);
                xmlBuffer.append(obj);
                wrapRightSign(xmlBuffer, key);
            }
        }
        return xmlBuffer.toString();
    }

    public static void wrapLeftSign(StringBuffer buf, String name) {
        buf.append("<").append(name).append(">");
    }

    public static void wrapRightSign(StringBuffer buf, String name) {
        buf.append("</").append(name).append(">");
    }
}