资源简介
wxPayUtil 这个一个微信的工具类,我在网上找到的,wxPayUtil 这个一个微信的工具类,我在网上找到的,wxPayUtil 这个一个微信的工具类,我在网上找到的,
代码片段和文件信息
package com.github.wxpay.sdk;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.*;
import java.security.MessageDigest;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import com.github.wxpay.sdk.WXPayConstants.SignType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class WXPayUtil {
/**
* xml格式字符串转换为Map
*
* @param strxml xml字符串
* @return xml数据转换后的Map
* @throws Exception
*/
public static Map xmlToMap(String strxml) throws Exception {
try {
Map data = new HashMap();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
InputStream stream = new ByteArrayInputStream(strxml.getBytes(“UTF-8“));
org.w3c.dom.Document doc = documentBuilder.parse(stream);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getDocumentElement().getChildNodes();
for (int idx = 0; idx < nodeList.getLength(); ++idx) {
Node node = nodeList.item(idx);
if (node.getNodeType() == Node.ELEMENT_NODE) {
org.w3c.dom.Element element = (org.w3c.dom.Element) node;
data.put(element.getNodeName() element.getTextContent());
}
}
try {
stream.close();
} catch (Exception ex) {
// do nothing
}
return data;
} catch (Exception ex) {
WXPayUtil.getLogger().warn(“Invalid xml can not convert to map. Error message: {}. xml content: {}“ ex.getMessage() strxml);
throw ex;
}
}
/**
* 将Map转换为xml格式的字符串
*
* @param data Map类型数据
* @return xml格式的字符串
* @throws Exception
*/
public static String mapToxml(Map data) throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder= documentBuilderFactory.newDocumentBuilder();
org.w3c.dom.Document document = documentBuilder.newDocument();
org.w3c.dom.Element root = document.createElement(“xml“);
document.appendChild(root);
for (String key: data.keySet()) {
String value = data.get(key);
if (value == null) {
- 上一篇:jsp操作mysql简单
- 下一篇:java金融项目开发-银行核心系统业务介绍
评论
共有 条评论