2023-02-06 11:52:26 +08:00

271 lines
9.7 KiB
Java

package com.tenwa.httpclient.pboc;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.amarsoft.are.ARE;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.*;
import java.util.*;
import java.util.regex.Pattern;
public class PbocXmlUtils {
/**
* 个人单笔查询的参数组装
* @param params
* @return
*/
public static String createPersonQueryParamStr(Map<String,String> params, String subjectId) {
/**
* String xmlParam = "<msgHead>" +
* "????<txCode>ZXCXA01</txCode>" +
* "????<reqSysCode>LOCAL</reqSysCode>" +
* "????<loginUserCode>system</loginUserCode>" +
* "????<loginPwd>123456</loginPwd>" +
* "????<finDept>a5adabc8ae00417bbe1b4f54423f4d03</finDept>" +
* "</msgHead>" +
* "<msgBody>" +
* "????<appType>01</appType>" +
* "????<rptUser>system</rptUser>" +
* "????<rptUserdept>777</rptUserdept>" +
* "????<custName>王小二</custName>" +
* "????<custCertype>10</custCertype>" +
* "????<custCertno>622926198501293785</custCertno>" +
* "????<qryReason>23</qryReason>" +
* "????<qryType>0</qryType>" +
* "????<qryFormat>01</qryFormat>" +
* "????<authStartDt>2021-03-25</authStartDt>" +
* "????<authEndDt>2023-03-24</authEndDt>" +
* "????<authArchFileNameList>爱啥啥</authArchFileNameList>" +
* "????<authArchUrl>www.baidu.com</authArchUrl>" +
* "????<certSrcUrl>www.sino.com</certSrcUrl>" +
* "????<certFileNameList>想啥啥</certFileNameList>" +
* "</msgBody>";
*/
try {
JSONObject param = new JSONObject();
JSONObject msgHead = new JSONObject();
JSONObject msgBody = new JSONObject();
msgHead.put("txCode", "ZXCXA01");
msgHead.put("reqSysCode", "LOCAL");
msgHead.put("loginUserCode", "system");
msgHead.put("loginPwd", "123456");
msgHead.put("finDept", "a5adabc8ae00417bbe1b4f54423f4d03");
param.put("msgHead", msgHead);
msgBody.put("appType", "01");
msgBody.put("rptUser", "system");
msgBody.put("rptUserdept", "777");
msgBody.put("custName", "王小二");
msgBody.put("custCertype", "10");
msgBody.put("custCertno", "622926198501293785");
msgBody.put("qryReason", "23");
msgBody.put("qryType", "0");
msgBody.put("qryFormat", "01");
msgBody.put("authStartDt", "2021-03-25");
msgBody.put("authEndDt", "2023-03-24");
msgBody.put("authArchFileNameList", "爱啥啥");
msgBody.put("authArchUrl", "www.baidu.com");
msgBody.put("certSrcUrl", "www.sino.com");
msgBody.put("certFileNameList", "想啥啥");
param.put("msgBody", msgBody);
ARE.getLog().info("xmlParamStr : " + param.toJSONString());
String xmlParamStr = toXml(param);
ARE.getLog().info("xmlParamStr : " + xmlParamStr);
return xmlParamStr;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 解决 CDATA 问题
private static Pattern pattern = Pattern.compile("[<>&\"',]");
public static String toXml(JSONObject obj) {
return jsonStr2Xml(obj.toJSONString());
}
private static String escape(String string) {
return pattern.matcher(string).find() ? "<![CDATA[" + string + "]]>" : string;
}
/**
* updateXml
* @param xmlStr
*/
private static String updateXml(String xmlStr) {
xmlStr = xmlStr.trim();
if (StringUtils.isBlank(xmlStr)) {
return xmlStr;
}
// 过滤非法字符
xmlStr = xmlStr.replaceAll("[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]", "");
StringBuilder xmlSb = new StringBuilder(xmlStr);
if (!xmlStr.startsWith("<?")) {
xmlSb.insert(0, "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
}
int idx = xmlSb.indexOf("?>") + 2;
xmlSb.insert(idx, "<root_luxsun>").append("</root_luxsun>");
return xmlSb.toString();
}
/**
* xml2Json
* @param xmlStr
* @return
* @throws DocumentException
*/
public static JSONObject toJson(String xmlStr) throws DocumentException {
xmlStr = updateXml(xmlStr);
Document doc = DocumentHelper.parseText(xmlStr);
JSONObject json = new JSONObject();
dom4j2Json(doc.getRootElement(), json);
return json;
}
/**
* toJsonString
* @param xmlStr
*/
public static String toJsonString(String xmlStr) throws DocumentException {
JSONObject jsonObject = toJson(xmlStr);
String json = JSON.toJSONString(jsonObject);
return json;
}
/**
* toMap
* @param xmlStr
*/
public static Map<String, Object> toMap(String xmlStr) throws DocumentException {
String jsonStr = toJsonString(xmlStr);
Map<String, Object> xmlMap = JSON.parseObject(jsonStr, LinkedHashMap.class);
return xmlMap;
}
/**
* xml转json
* @param element
* @param json
*/
private static void dom4j2Json(Element element, JSONObject json){
// 如果是属性
for(Object o:element.attributes()){
Attribute attr=(Attribute)o;
if(!isEmpty(attr.getValue())){
json.put("@"+attr.getName(), attr.getValue());
}
}
List<Element> chdEl=element.elements();
if(chdEl.isEmpty()&&!isEmpty(element.getText())){ // 如果没有子元素,只有一个值
json.put(element.getName(), element.getText());
}
for(Element e:chdEl){ // 有子元素
if(!e.elements().isEmpty()){ // 子元素也有子元素
JSONObject chdjson=new JSONObject();
dom4j2Json(e,chdjson);
Object o=json.get(e.getName());
if(o!=null){
JSONArray jsona=null;
if(o instanceof JSONObject){ // 如果此元素已存在,则转为jsonArray
JSONObject jsono=(JSONObject)o;
json.remove(e.getName());
jsona=new JSONArray();
jsona.add(jsono);
jsona.add(chdjson);
}
if(o instanceof JSONArray){
jsona=(JSONArray)o;
jsona.add(chdjson);
}
json.put(e.getName(), jsona);
}else{
if(!chdjson.isEmpty()){
json.put(e.getName(), chdjson);
}
}
}else{ // 子元素没有子元素
for(Object o:element.attributes()){
Attribute attr=(Attribute)o;
if(!isEmpty(attr.getValue())){
json.put("@"+attr.getName(), attr.getValue());
}
}
if(!e.getText().isEmpty()){
json.put(e.getName(), e.getText());
}
}
}
}
public static boolean isEmpty(String str) {
if (str == null || str.trim().isEmpty() || "null".equals(str)) {
return true;
}
return false;
}
/**
* jsonStr2Xml
* @param json
* @return java.lang.String
*/
public static String jsonStr2Xml(String json){
try {
StringBuffer buffer = new StringBuffer();
buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
JSONObject jObj = JSON.parseObject(json);
json2Xmlstr(jObj,buffer);
return buffer.toString();
}
catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* Json to xmlstr string
*
* @param jObj the j obj
* @param buffer the buffer
* @return the string
*/
public static String json2Xmlstr(JSONObject jObj,StringBuffer buffer){
Set<Map.Entry<String, Object>> se = jObj.entrySet();
for(Iterator<Map.Entry<String, Object>> it = se.iterator(); it.hasNext(); )
{
Map.Entry<String, Object> en = it.next();
if(en.getValue().getClass().getName().equals("com.alibaba.fastjson.JSONObject")){
buffer.append("<"+en.getKey()+">");
JSONObject jo = jObj.getJSONObject(en.getKey());
json2Xmlstr(jo,buffer);
buffer.append("</"+en.getKey()+">");
}else if(en.getValue().getClass().getName().equals("com.alibaba.fastjson.JSONArray")){
JSONArray jarray = jObj.getJSONArray(en.getKey());
for (int i = 0; i < jarray.size(); i++) {
buffer.append("<"+en.getKey()+">");
JSONObject jsonobject = jarray.getJSONObject(i);
json2Xmlstr(jsonobject,buffer);
buffer.append("</"+en.getKey()+">");
}
}else if(en.getValue().getClass().getName().equals("java.lang.String")){
buffer.append("<"+en.getKey()+">" + escape((String) en.getValue()));
buffer.append("</"+en.getKey()+">");
}
}
return buffer.toString();
}
}