package com.tenwa.httpclient.pypafhtml; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.dom4j.tree.DefaultAttribute; import java.io.File; import java.io.FileOutputStream; import java.io.PrintStream; import java.util.List; public class PyHtmlCreate { /** * xml文件读取并生成html文件 * @param xmlFilePath * @return */ public static String createHtmlFile(String xmlFilePath, String htmlFilePath) throws Exception { SAXReader reader = new SAXReader(); File xmlFile = new File(xmlFilePath); Document document = reader.read(xmlFile); Element root = document.getRootElement(); JSONObject obj = createObjJson(root); System.out.println("PyHtmlCreate===========JSON=" + obj.toJSONString()); String reportId = obj.getString("reportID"); // TODO: 2020/7/30 文件地址修改 htmlFilePath = htmlFilePath + "xmlHtml/"+ reportId +".html"; File htmlFile = new File(htmlFilePath); htmlFile.createNewFile(); String htmlContent = createHtml(obj); PrintStream printStream = new PrintStream(new FileOutputStream(htmlFile)); printStream.println(htmlContent);//将字符串写入文件 return htmlFilePath; } /** * xml转换json * @param root * @return */ private static JSONObject createObjJson(Element root){ JSONObject obj = new JSONObject(); List rootAttribute = root.attributes(); for(DefaultAttribute roAttribute : rootAttribute) { obj.put(roAttribute.getName(), roAttribute.getStringValue()); } List cisReports = root.elements(); String reportID = null; for(Element cisReport : cisReports) { List crAttributes = cisReport.attributes(); for(DefaultAttribute crAttribute : crAttributes) { obj.put(crAttribute.getName(), crAttribute.getStringValue()); } JSONObject queryConditionsObj = new JSONObject(); obj.put("queryConditionsObj", queryConditionsObj); Element queryCondition = cisReport.element("queryConditions"); List items = queryCondition.elements("item"); for(Element item : items) { List subItem = item.elements(); queryConditionsObj.put(subItem.get(0).getTextTrim(), subItem.get(2).getTextTrim()); } JSONObject personAntiFraudDescInfo = createJson(cisReport, "personAntiFraudDescInfo"); obj.put("personAntiFraudDescInfo", personAntiFraudDescInfo); JSONObject personAntiFraudScoreInfo = createJson(cisReport, "personAntiFraudScoreInfo"); obj.put("personAntiFraudScoreInfo", personAntiFraudScoreInfo); JSONObject personBaseInfo = createJson(cisReport, "personBaseInfo"); obj.put("personBaseInfo", personBaseInfo); JSONObject personRiskRosterInfo = createJson(cisReport, "personRiskRosterInfo"); obj.put("personRiskRosterInfo", personRiskRosterInfo); JSONObject policeCheckInfo = createJson(cisReport, "policeCheckInfo"); obj.put("policeCheckInfo", policeCheckInfo); JSONObject mobileCheckInfo = createJson(cisReport, "mobileCheckInfo"); obj.put("mobileCheckInfo", mobileCheckInfo); JSONObject mobileStatusInfo = createJson(cisReport, "mobileStatusInfo"); obj.put("mobileStatusInfo", mobileStatusInfo); JSONObject personJudicialRiskInfo = createJson(cisReport, "personJudicialRiskInfo"); obj.put("personJudicialRiskInfo", personJudicialRiskInfo); } return obj; } /** * json 组装 * @param cisReport * @param key * @return */ private static JSONObject createJson(Element cisReport, String key){ JSONObject obj = new JSONObject(); Element subElement = cisReport.element(key); List subAttributes = subElement.attributes(); for(DefaultAttribute sub : subAttributes) { obj.put(sub.getName(), sub.getStringValue()); } //返回结果值不为3即尝试解析内容是否有值 if(!"3".equals(obj.getString("treatResult"))){ List grandsonElements = subElement.elements(); JSONArray item = new JSONArray(); for(Element element : grandsonElements) { if(!"item".equals(element.getName())){ obj.put(element.getName(), element.getStringValue().trim()); }else { JSONObject itemObj = new JSONObject(); List subGrandsonElements = element.elements(); for(Element e : subGrandsonElements){ itemObj.put(e.getName(), e.getStringValue().trim()); } item.add(itemObj); } } if(null != item && item.size() > 0){ obj.put("item", item); } } return obj; } /** * html组装 * @param obj * @return */ private static String createHtml(JSONObject obj) throws Exception { StringBuilder sb = new StringBuilder(); sb.append(PyHtmlBase.header); sb.append(PyHtmlContent.createBodyHead(obj)); sb.append(PyHtmlContent.createBodyContent(obj)); sb.append(PyHtmlBase.tail); return sb.toString(); } }