微信支付中xml和map互转

前端之家收集整理的这篇文章主要介绍了微信支付中xml和map互转前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

现在用java做微信公众号的支付,支付过程将参数发给微信是以xml格式发送,接受过来的也是xml格式字符串。感觉网上的xml和map转化复杂就手写了一个,具体代码如下:

package com.ciji.zzaclient.util.weixin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/** * XML与JavaBean相互转换工具类 */
public final class XMLBeanUtils {

    public static void main(String[] args) {
        Map<String,String> map = new HashMap<String,String>();
        /* * map.put("appid",WeixinUtils.APP_ID); map.put("mch_id",* WeixinUtils.MCH_ID); */
        map.put("device_info ","WEB");
        //String nonce_str = RandomStringGenerator.getRandomStringByLength(32);
        map.put("nonce_str","5K8264ILTKCH16CQ2502SI8ZNMTM67VS");
        map.put("body","商品描述。XX项目");
        map.put("out_trade_no","001t" + System.currentTimeMillis());
        map.put("total_fee","0.01");
        map.put("trade_type","JSAPI");
        map.put("openid","o4lmljscgZDnjI4xthqMMnEr02fo");

        String xmlResult = "";

        StringBuffer sb = new StringBuffer();
        sb.append("<xml>");
        for (String key : map.keySet()) {
            System.out.println(key + "========" + map.get(key));
            sb.append("<" + key + ">" + map.get(key) + "</" + key + ">");
            System.out.println();
        }
        sb.append("</xml>");
        xmlResult = sb.toString();
        System.out.println(xmlResult);

    }

    public static String map2XmlString(Map<String,String> map) {
        String xmlResult = "";

        StringBuffer sb = new StringBuffer();
        sb.append("<xml>");
        for (String key : map.keySet()) {
            System.out.println(key + "========" + map.get(key));

            String value = "<![CDATA[" + map.get(key) + "]]>";
            sb.append("<" + key + ">" + value + "</" + key + ">");
            System.out.println();
        }
        sb.append("</xml>");
        xmlResult = sb.toString();

        return xmlResult;
    }

    /** * @description 将xml字符串转换成map * @param xml * @return Map */
    public static Map<String,String> readStringXmlOut(String xml) {
        Map<String,String>();
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(xml); // 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点
            @SuppressWarnings("unchecked")
            List<Element> list = rootElt.elements();// 获取根节点下所有节点
            for (Element element : list) { // 遍历节点
                map.put(element.getName(),element.getText()); // 节点的name为map的key,text为map的value
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

}

效果是将map值转为如下的xml:

<xml>
  <nonce_str>ojc0du21prhs97xsazmjxynhp11oun41</nonce_str>
  <out_trade_no>001t144740390279</out_trade_no>
  <device_info>WEB</device_info>
  <openid>o4lmljscgZDnjI4xthqMMnEr02fo</openid>
  <total_fee>0.01</total_fee>
  <trade_type>JSAPI</trade_type>
  <body>商品描述。xxx项目</body>
</xml>

猜你在找的XML相关文章