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 com.tenwa.flow.util.CalculateUtil; import com.tenwa.httpclient.resources.BigDataPropertiesUtil; import org.apache.commons.lang3.StringUtils; import org.dom4j.*; import java.text.SimpleDateFormat; import java.util.*; import java.util.regex.Pattern; public class PbocXmlUtils { /** * 个人单笔查询的参数组装 * @return */ public static String createPersonQueryParamStr(Map params, String subjectId) { try { JSONObject param = new JSONObject(); JSONObject msgHead = new JSONObject(); JSONObject msgBody = new JSONObject(); msgHead.put("txCode", BigDataPropertiesUtil.get("pboc_txCode")); msgHead.put("reqSysCode", BigDataPropertiesUtil.get("pboc_reqSysCode")); msgHead.put("loginUserCode", BigDataPropertiesUtil.get("pboc_loginUserCode")); msgHead.put("loginPwd", BigDataPropertiesUtil.get("pboc_loginPwd")); msgHead.put("finDept", BigDataPropertiesUtil.get("pboc_finDept")); param.put("msgHead", msgHead); //查询申请类型 01-人工申请(通过前置系统发起的申请) msgBody.put("appType", "01"); msgBody.put("rptUser", BigDataPropertiesUtil.get("pboc_loginUserCode"));//报告使用人 登录征信查询前置系统的用户账号 msgBody.put("rptUserdept", "777");//填写部门机构代码 // msgBody.put("reqId", applyId); msgBody.put("custName", params.get("fullName")); msgBody.put("custCertype", "10");//证件类型 10-身份证 msgBody.put("custCertno", params.get("fullCertId")); //查询原因 24-融资审批 msgBody.put("qryReason", "24"); msgBody.put("qryType", "0");//查询类型 msgBody.put("rptQryType", "1");//信用报告查询方式:1本地优先,2仅查询本地,3仅查询征信系统。 //"01-银行版 //02-自助查询版 //03-政府版 //04-社会版" msgBody.put("qryFormat", "01");//查询版本 /** * a-基本信息 * b-信息概要 * e-公共信息明细 */ // msgBody.put("rptRstSegment", "a|b|e"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = new Date(); String startTime = sdf.format(date); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.YEAR, 1); String endTime = sdf.format(cal.getTime()); msgBody.put("authStartDt", startTime); msgBody.put("authEndDt", endTime); msgBody.put("authArchUrl", BigDataPropertiesUtil.get("pboc_authArchUrl"));//授权档案来源URL msgBody.put("certSrcUrl", BigDataPropertiesUtil.get("pboc_certSrcUrl"));//证照来源URL // msgBody.put("certFileNameList", "证照文件名称"); // msgBody.put("authArchFileNameList", "授权档案文件名称"); 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(); } }