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 params, String subjectId) { /** * String xmlParam = "" + * "????ZXCXA01" + * "????LOCAL" + * "????system" + * "????123456" + * "????a5adabc8ae00417bbe1b4f54423f4d03" + * "" + * "" + * "????01" + * "????system" + * "????777" + * "????王小二" + * "????10" + * "????622926198501293785" + * "????23" + * "????0" + * "????01" + * "????2021-03-25" + * "????2023-03-24" + * "????爱啥啥" + * "????www.baidu.com" + * "????www.sino.com" + * "????想啥啥" + * ""; */ 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() ? "" : 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(""); } int idx = xmlSb.indexOf("?>") + 2; xmlSb.insert(idx, "").append(""); 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 toMap(String xmlStr) throws DocumentException { String jsonStr = toJsonString(xmlStr); Map 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 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(""); 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> se = jObj.entrySet(); for(Iterator> it = se.iterator(); it.hasNext(); ) { Map.Entry 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(""); }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(""); } }else if(en.getValue().getClass().getName().equals("java.lang.String")){ buffer.append("<"+en.getKey()+">" + escape((String) en.getValue())); buffer.append(""); } } return buffer.toString(); } }