mirror of
https://gitee.com/egzosn/pay-java-parent.git
synced 2026-05-07 19:46:15 +08:00
XML多层解析,map转xml字符串
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
AliPayConfigStorage aliPayConfigStorage = new AliPayConfigStorage();
|
||||
aliPayConfigStorage.setPid("合作者id");
|
||||
aliPayConfigStorage.setAppId("应用id");
|
||||
aliPayConfigStorage.setAliPublicKey("支付宝公钥");
|
||||
aliPayConfigStorage.setKeyPublic("支付宝公钥");
|
||||
aliPayConfigStorage.setKeyPrivate("应用私钥");
|
||||
aliPayConfigStorage.setNotifyUrl("异步回调地址");
|
||||
aliPayConfigStorage.setReturnUrl("同步回调地址");
|
||||
|
||||
@@ -37,7 +37,7 @@ import static com.egzosn.pay.common.http.UriVariables.getMapToParameters;
|
||||
*/
|
||||
public class ClientHttpRequest<T> extends HttpEntityEnclosingRequestBase implements org.apache.http.client.ResponseHandler<T>{
|
||||
protected static final Log LOG = LogFactory.getLog(ClientHttpRequest.class);
|
||||
public static final ContentType APPLICATION_FORM_URLENCODED_UTF_8 = ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8);;
|
||||
public static final ContentType APPLICATION_FORM_URLENCODED_UTF_8 = ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8);
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.egzosn.pay.common.bean.result.PayException;
|
||||
import com.egzosn.pay.common.exception.PayErrorException;
|
||||
import com.egzosn.pay.common.util.str.StringUtils;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
@@ -254,36 +255,49 @@ public class XML {
|
||||
* @return XML格式的字符串
|
||||
*/
|
||||
public static String getMap2Xml(Map<String, Object> data) {
|
||||
return getMap2Xml(data, "xml", "UTF-8");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将Map转换为XML格式的字符串
|
||||
*
|
||||
* @param data Map类型数据
|
||||
* @param rootElementName 最外层节点名称
|
||||
* @return XML格式的字符串
|
||||
*/
|
||||
public static String getMap2Xml(Map<String, Object> data, String rootElementName, String encoding) {
|
||||
Document document = null;
|
||||
try {
|
||||
document = newDocument();
|
||||
} catch (ParserConfigurationException e) {
|
||||
throw new PayErrorException(new PayException("ParserConfigurationException", e.getLocalizedMessage()));
|
||||
}
|
||||
org.w3c.dom.Element root = document.createElement("xml");
|
||||
org.w3c.dom.Element root = document.createElement(rootElementName);
|
||||
document.appendChild(root);
|
||||
for (Map.Entry<String, Object> entry : data.entrySet()) {
|
||||
/* for (Map.Entry<String, Object> entry : data.entrySet()) {
|
||||
Object value = entry.getValue();
|
||||
if (value == null) {
|
||||
value = "";
|
||||
}
|
||||
|
||||
value = value.toString().trim();
|
||||
org.w3c.dom.Element filed = document.createElement(entry.getKey());
|
||||
filed.appendChild(document.createTextNode(value.toString()));
|
||||
root.appendChild(filed);
|
||||
}
|
||||
}*/
|
||||
|
||||
map2Xml(data, document, root);
|
||||
try {
|
||||
TransformerFactory tf = TransformerFactory.newInstance();
|
||||
Transformer transformer = tf.newTransformer();
|
||||
DOMSource source = new DOMSource(document);
|
||||
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
||||
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
StringWriter writer = new StringWriter();
|
||||
StreamResult result = new StreamResult(writer);
|
||||
transformer.transform(source, result);
|
||||
String output = writer.getBuffer().toString(); //.replaceAll("\n|\r", "");
|
||||
String output = writer.getBuffer().toString();
|
||||
return output;
|
||||
} catch (TransformerException e) {
|
||||
e.printStackTrace();
|
||||
@@ -293,5 +307,27 @@ public class XML {
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将Map转换为XML格式的字符串
|
||||
*
|
||||
* @param data Map类型数据
|
||||
* @param document 文档
|
||||
* @return XML格式的字符串
|
||||
*/
|
||||
public static void map2Xml(Map<String, Object> data, Document document, org.w3c.dom.Element element) {
|
||||
for (Map.Entry<String, Object> entry : data.entrySet()) {
|
||||
Object value = entry.getValue();
|
||||
if (value == null) {
|
||||
value = "";
|
||||
}
|
||||
org.w3c.dom.Element filed = document.createElement(entry.getKey());
|
||||
if (value instanceof Map){
|
||||
map2Xml((Map)value, document, filed);
|
||||
}else {
|
||||
value = value.toString().trim();
|
||||
filed.appendChild(document.createTextNode(value.toString()));
|
||||
}
|
||||
element.appendChild(filed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public enum PayType implements BasePayType {
|
||||
AliPayConfigStorage aliPayConfigStorage = new AliPayConfigStorage();
|
||||
aliPayConfigStorage.setPid(apyAccount.getPartner());
|
||||
aliPayConfigStorage.setAppId(apyAccount.getAppid());
|
||||
aliPayConfigStorage.setAliPublicKey(apyAccount.getPublicKey());
|
||||
aliPayConfigStorage.setKeyPublic(apyAccount.getPublicKey());
|
||||
aliPayConfigStorage.setKeyPrivate(apyAccount.getPrivateKey());
|
||||
aliPayConfigStorage.setNotifyUrl(apyAccount.getNotifyUrl());
|
||||
aliPayConfigStorage.setReturnUrl(apyAccount.getReturnUrl());
|
||||
|
||||
Reference in New Issue
Block a user