All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lorne.weixin.pay.model.MapXml Maven / Gradle / Ivy

package com.lorne.weixin.pay.model;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yuliang on 2016/11/16.
 */
public abstract class MapXml {

    public String toXml() {
        StringBuilder sb = new StringBuilder();
        sb.append("");
        sb.append("");
        Map map = new HashMap();
        Field[] fields = getClass().getDeclaredFields();
        for (Field field : fields) {
            Object obj;
            try {
                field.setAccessible(true);
                obj = field.get(this);
                if (obj != null) {
                    if (obj instanceof String) {
                        String str = (String) obj;
                        if (!"".equals(str)) {
                            map.put(field.getName(), obj);
                            sb.append("<" + field.getName() + ">");
                        }
                    } else {
                        map.put(field.getName(), obj);
                        sb.append("<" + field.getName() + ">");
                    }
                }
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        sb.append("");
        return sb.toString();
    }


    public Map toMap() {
        Map map = new HashMap();
        Field[] fields = getClass().getDeclaredFields();
        for (Field field : fields) {
            Object obj;
            try {
                field.setAccessible(true);
                obj = field.get(this);
                if (obj != null) {
                    if (obj instanceof String) {
                        String str = (String) obj;
                        if (!"".equals(str)) {
                            map.put(field.getName(), obj);
                        }
                    } else {
                        map.put(field.getName(), obj);
                    }
                }
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return map;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy