xml样例
<microNearlyThreeYearsOverdueInfo subReportType="13204" subReportTypeCost="96040" treatResult="子报告查询状态,1:查得,2:未查得,3:其他原因未查得" treatErrorCode="treatResult=3时的错误代码,详见数据字典,treatResult!=3时,该属性不存在" errorMessage="treatResult=3时的错误描述信息,treatResult!=3时,该属性的值为空">
<!--treatresult为2、3时没有以下节点-->
<!--逾期汇总信息 0..1-->
<stat>
<overdueAmount>逾期本金,单位:以贷款基本信息的币种为准</overdueAmount>
<overdueDays>逾期天数区间,取值区间:逾期1天(含)以上</overdueDays>
<overdueCount>逾期天数区间的总笔数</overdueCount>
</stat>
</microNearlyThreeYearsOverdueInfo>
定义实体并解析
父类:CommonField
public class CommonField extends BasePojo { private String treatResult;//子报告查询状态,1:查得,2:未查得,3:其他原因未查得 private String errorMessage;//treatResult=3时的错误描述信息,treatResult!=3时,该属性的值为空
子类:MicroNearlyThreeYearsOverdueInfo
public class MicroNearlyThreeYearsOverdueInfo extends CommonField implements Serializable { private String overdueAmount;//逾期本金,单位:以贷款基本信息的币种为准 private String overdueDays;//逾期天数区间,取值区间:逾期1-6天,逾期7-29天,逾期30-59天,逾期60-89天,逾期90天(含)以上 private Integer overdueCount;//逾期天数区间的笔数
一,解析节点属性
Element microNearlyThreeYearsOverdueInfoElement = cisReport.element("microNearlyThreeYearsOverdueInfo"); List<Attribute> microNearlyThreeYearsOverdueInfoAttributeList = microNearlyThreeYearsOverdueInfoElement .attributes();
//(1)把属性数据复制给实体 先转map MicroNearlyThreeYearsOverdueInfo mtyoi = getMicroNearlyThreeYearsOverdueInfoNode(microNearlyThreeYearsOverdueInfoElement, microNearlyThreeYearsOverdueInfoAttributeList); re.setMicroNearlyThreeYearsOverdueInfo(mtyoi);
//(1)把属性数据复制给实体 先转map
private MicroNearlyThreeYearsOverdueInfo getMicroNearlyThreeYearsOverdueInfoNode(Element element, List<Attribute> attributeList) throws Exception { try { // 获取网贷逾期信息的全部属性 Map<String, String> attributeMap = attributeList.stream() .collect(Collectors.toMap(Attribute::getName, Attribute::getValue)); //(2)属性赋值给父类 MicroNearlyThreeYearsOverdueInfo obj = (MicroNearlyThreeYearsOverdueInfo) CommonUtils.setValueOfSuperClass(MicroNearlyThreeYearsOverdueInfo.class, attributeMap); if ("1".equals(obj.getTreatResult())) { //(3)解析子节点-逾期汇总信息 getMicroNearlyThreeYearsOverDueTotalInfo(element, obj); // 详细信息 getMicroNearlyThreeYearsOverdueSummaryInfo(element, obj); } return obj; } catch (Exception e) { logger.error("getMicroNearlyThreeYearsOverdueInfoNode@PyserviceImpl_Exception", e); throw e; } }
(2)属性赋值给父类
/** * @param clazz, attributeMap * @return java.lang.Object * @Description: 通过反射给目标对象的父类设置属性 * @date 2017/8/30 9:42 */ public static Object setValueOfSuperClass(Class<?> clazz, Map<String, String> attributeMap) throws Exception { try { Object object = Class.forName(clazz.getName()).newInstance(); Class<?> obj = object.getClass().getSuperclass(); Field[] fields = obj.getDeclaredFields(); setValue(object, attributeMap, fields); return object; } catch (Exception e) { logger.info(e.getMessage()); throw e; } }
二,解析子节点内容
上://(3)解析子节点-逾期汇总信息
private MicroNearlyThreeYearsOverdueInfo getMicroNearlyThreeYearsOverDueTotalInfo(Element element, MicroNearlyThreeYearsOverdueInfo obj) { Element eStat = element.element("stat"); if (eStat != null) { List<Element> statlist = eStat.elements(); //解析节点内容 //方式一,遍历List<Element>,调用e.elementText() if (!ListUtil.isEmpty(statlist)) { for (Element e : statlist) { String overdueAmount = e.elementText("overdueAmount"); String overdueDays = e.elementText("overdueDays"); String overdueCount = e.elementText("overdueCount"); } } //方式二,List<Element>转化为map<节点名称,节点值> if (!ListUtil.isEmpty(statlist)) { //(4)List<Element>转化为map<节点名称,节点值> Map<String, String> resultMap = XmlUtil.getResultMap(statlist); obj.setOverdueAmount(resultMap.get("overdueAmount")); obj.setOverdueDays(resultMap.get("overdueDays")); obj.setOverdueCount(Integer.valueOf(resultMap.get("overdueCount"))); } } return obj; }
/** * 将元素节点转换成map */
(4)List<Element>转化为map<节点名称,节点值> public static Map<String, String> getResultMap(List<Element> items) { Map<String, String> map = new HashMap<>(32); for (Element e : items) { if (StringUtils.isNotEmpty(e.getData().toString())) { map.put(e.getName(), e.getData().toString()); } } return map; }