wanghc
2023-03-03 5378165da6ad9f748a8a3e4599743d0bfbb4835b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.jttech.cmci.pfcs.util;
 
import com.alibaba.fastjson.JSONObject;
import com.jttech.cmci.pfcs.vo.req.ZxmxGetScoreReqVo;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.springframework.util.StringUtils;
 
 
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
 
/**
 * @author Skaði the Corrupting Heart
 * @version 1.0.0
 * @ClassName xml.java
 * @Description TODO
 * @createTime 2022年04月02日 17:12
 */
public class XmlUtils {
 
    public static JSONObject xml2Json(String xmlStr) {
        try {
            if (StringUtils.isEmpty(xmlStr)) {
                return null;
            }
            xmlStr = xmlStr.replaceAll("\\\n", "");
            byte[] xml = xmlStr.getBytes("UTF-8");
            JSONObject json = new JSONObject();
            InputStream is = new ByteArrayInputStream(xml);
            SAXBuilder sb = new SAXBuilder();
            Document doc = sb.build(is);
            Element root = doc.getRootElement();
            json.put(root.getName(), iterateElement(root));
 
            return json;
        } catch (Exception pE) {
            throw new RuntimeException("xml文件解析失败", pE);
        }
 
 
    }
 
 
    private static JSONObject iterateElement(Element element) {
        List<Element> node = element.getChildren();
        JSONObject obj = new JSONObject();
        List list = null;
        for (Element child : node) {
            list = new LinkedList();
            String text = child.getTextTrim();
            if (StringUtils.isEmpty(text)) {
                if (child.getChildren().size() == 0) {
                    continue;
                }
                if (obj.containsKey(child.getName())) {
                    list = (List) obj.get(child.getName());
                }
                list.add(iterateElement(child)); // 遍历child的子节点
                obj.put(child.getName(), list);
            } else {
                if (obj.containsKey(child.getName())) {
                    Object value = obj.get(child.getName());
                    try {
                        list = (List) value;
                    } catch (ClassCastException e) {
                        list.add(value);
                    }
                }
                if (child.getChildren().size() == 0) { // child无子节点时直接设置text
                    obj.put(child.getName(), text);
                } else {
                    list.add(text);
                    obj.put(child.getName(), list);
                }
            }
        }
        return obj;
 
    }
 
 
    public static void main(String[] args) {
        String a = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ReportMessage><ReportHead><ReportIdentInfo><ReportId>2022051909455761166981</ReportId><ReportTime>2022.05.19 09:45:57</ReportTime></ReportIdentInfo><ThisQueryReqInfo><QueryName>罗玉林</QueryName><QueryCertType>身份证</QueryCertType><QueryCredNum>522424200009281017</QueryCredNum><QueryOrg>深圳市中裔信息工程融资担保有限公司</QueryOrg><QueryReasonCode>担保资格审查</QueryReasonCode></ThisQueryReqInfo><ObjPromptMsg><Info>信息主体对信用报告内容提出了0笔异议且正在处理中,请浏览时注意阅读相关内容。</Info></ObjPromptMsg></ReportHead><BaseInfo><IdentityInfo><SurveyInfo><Gender>男</Gender><DateOfBirth>2000.09.28</DateOfBirth><MaritalStatus>未婚</MaritalStatus><Education>初中及以下</Education><Degree>其他</Degree><WorkStatus>--</WorkStatus><Citizenship>--</Citizenship><Email>--</Email><ContactAddress>广东揭阳揭东区磐东镇河中村新世纪商场附近</ContactAddress><ResidenceAddress>--</ResidenceAddress></SurveyInfo><PhoneInfo><No>1</No><Phone>17685170689</Phone><InfoUpdateDate>2018.10.04</InfoUpdateDate></PhoneInfo></IdentityInfo><ResideInfo><No>1</No><ResideAddr>广东揭阳揭东区磐东镇河中村新世纪商场附近</ResideAddr><ResidePhone>--</ResidePhone><ResideStatus>--</ResideStatus><InfoUpdateDate>2018.10.04</InfoUpdateDate></ResideInfo></BaseInfo><ReportSumamry><CreditTranPromptInfo><No>1</No><BusinessType>个人住房贷款</BusinessType><AccountNum>--</AccountNum><FirstBusinessReleaseMonth>--</FirstBusinessReleaseMonth></CreditTranPromptInfo><CreditTranPromptInfo><No>2</No><BusinessType>个人商用房贷款(包括商住两用房)</BusinessType><AccountNum>--</AccountNum><FirstBusinessReleaseMonth>--</FirstBusinessReleaseMonth></CreditTranPromptInfo><CreditTranPromptInfo><No>3</No><BusinessType>其他类贷款</BusinessType><AccountNum>1</AccountNum><FirstBusinessReleaseMonth>2018.10</FirstBusinessReleaseMonth></CreditTranPromptInfo><CreditTranPromptInfo><No>4</No><BusinessType>贷记卡</BusinessType><AccountNum>--</AccountNum><FirstBusinessReleaseMonth>--</FirstBusinessReleaseMonth></CreditTranPromptInfo><CreditTranPromptInfo><No>5</No><BusinessType>准贷记卡</BusinessType><AccountNum>--</AccountNum><FirstBusinessReleaseMonth>--</FirstBusinessReleaseMonth></CreditTranPromptInfo><CreditTranPromptInfo><No>6</No><BusinessType>--</BusinessType><AccountNum>--</AccountNum><FirstBusinessReleaseMonth>--</FirstBusinessReleaseMonth></CreditTranPromptInfo><CreditTranPromptInfo><No>7</No><BusinessType>合计</BusinessType><AccountNum>1</AccountNum><FirstBusinessReleaseMonth>--</FirstBusinessReleaseMonth></CreditTranPromptInfo><CreditDefaultSumInfo><BadDebtsSummInfo><AccountNum>1</AccountNum><Balance>1,919</Balance></BadDebtsSummInfo></CreditDefaultSumInfo><CreditTranAndDebtSummInfo><NonrevolvingLoanSummInfo><OrgNum>1</OrgNum><AccountNum>1</AccountNum><AuthorizationTotal>2,665</AuthorizationTotal><Balance>1,919</Balance><AvgRepaymentLastSixMonth>2,170</AvgRepaymentLastSixMonth></NonrevolvingLoanSummInfo><RevolvingLoanAccountSummInfo/></CreditTranAndDebtSummInfo><QueryRecordSummary><LastQueryRecordInfo><LastQueryDate>2022.04.12</LastQueryDate><LastQueryOrg>商业银行\"CW\"</LastQueryOrg><LastQueryReason>信用卡审批</LastQueryReason></LastQueryRecordInfo><QueryRecordSummInfo><NumQueryOrgLoanApprovalLastOneMonth>0</NumQueryOrgLoanApprovalLastOneMonth><NumQueryOrgCreditCardApprovalLastOneMonth>0</NumQueryOrgCreditCardApprovalLastOneMonth><NumQueryLoanApprovalLastOneMonth>0</NumQueryLoanApprovalLastOneMonth><NumQueryCreditCardApprovalLastOneMonth>0</NumQueryCreditCardApprovalLastOneMonth><NumQuerySelfQueryLastOneMonth>0</NumQuerySelfQueryLastOneMonth><NumQueryPostLoanManagementLastTwoYear>3</NumQueryPostLoanManagementLastTwoYear><NumQueryGuaranteeQualificationReviewLastTwoYear>0</NumQueryGuaranteeQualificationReviewLastTwoYear><NumQuerySpecialMerchantRealNameReviewLastTwoYear>0</NumQuerySpecialMerchantRealNameReviewLastTwoYear></QueryRecordSummInfo></QueryRecordSummary></ReportSumamry><CreditTranDetailsInfo><NonrevolvingLoanDetInfo><BaseInfo><CreditPrtlIdent>账户</CreditPrtlIdent><ManagmtOrg>消费金融公司\"YY\"</ManagmtOrg><AccountId>******</AccountId><IssuanceDate>2018.10.04</IssuanceDate><DueDate>2019.08.31</DueDate><Money>2,665</Money><Currency>人民币元</Currency><Type>其他个人消费贷款</Type><GuratMode>信用/免担保</GuratMode><RepayPerid>10</RepayPerid><RepayFrequency>月</RepayFrequency><RepayType>--</RepayType><CommonBwMark>无</CommonBwMark></BaseInfo><LatestPerformanceInfo><Title>截至2022年04月30日</Title><AccountStatus>呆账</AccountStatus><Balance>1,919</Balance><LastRepayDate>2019.02.20</LastRepayDate></LatestPerformanceInfo><Last5YearsHisPerFmInfo><Title>2018年10月 —2022年04月的还款记录</Title><Content>7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|7_2,170|6_1,856|5_0|4_1,232|3_920|2_608|1_296|N_0|N_0|N_0|*_0|</Content></Last5YearsHisPerFmInfo></NonrevolvingLoanDetInfo></CreditTranDetailsInfo><NonCreditTranDetailsInfo/><PublicInfo/><OpenInfo/><QueryInfo><No>1</No><QueryDate>2022.04.12</QueryDate><QueryOrg>商业银行\"CW\"</QueryOrg><Reason>信用卡审批</Reason></QueryInfo><QueryInfo><No>2</No><QueryDate>2020.10.29</QueryDate><QueryOrg>商业银行\"AV\"</QueryOrg><Reason>信用卡审批</Reason></QueryInfo><QueryInfo><No>3</No><QueryDate>2020.10.23</QueryDate><QueryOrg>小额贷款公司\"KU\"</QueryOrg><Reason>贷款审批</Reason></QueryInfo><QueryInfo><No>4</No><QueryDate>2020.10.07</QueryDate><QueryOrg>消费金融公司\"QY\"</QueryOrg><Reason>贷款审批</Reason></QueryInfo><RptExplain><Title>报告说明</Title><Content>1. 本报告中的“数字解读”仅供使用本信用报告的银行等授信机构参考,授信机构应自行承担使用“数字解读”的相关法律责任。2. “数字解读”将信用报告内容解读为一个数值,是对信用主体未来信贷违约可能性的预测,其取值范围为 0 到 1000,分值越高,违约可能性越低;“相对位置”是信用主体的数字解读值在全部人群中的百分比排序位置,比如“&gt;50%”代表该数字解读值高于 50%的信用主体;“说明”中的“影响因素”是影响信用主体获得更高数字解读值的原因,根据当前信用报告的实际情况给出,最多有两条。“数字解读”显示为“--”的,仅代表无法根据当前信用报告内容给出数字解读值,并无其他含义。无法给出数字解读值的具体原因见“说明” 。3. 本报告的信贷交易信息提示中,“业务类型”为“其他” 的汇总信息不包含“资产处置” 和“垫款” 业务。4. 本报告中如果没有“信贷交易违约信息概要”信息,说明信用主体最近 5 年内没有连续逾期。5. 对于存在授信限额的协议信息,信息主体的可用额度需结合“授信协议信息” 中的授信额度、 授信限额信息和余额进行估算。6. 本报告中的信贷交易授信及负债信息概要展示的信息是指未结清/未销户的授信及负债信息。7. 本报告的借贷交易明细信息中,循环贷账户的到期日期是指账户授信额度的到期日期。8. 本报告的借贷交易明细信息中,借贷账户展示最近 5 年的还款情况,包括当前还款状态和当前逾期总额。9.对于通过自助渠道办理的“小额、 高频” 业务,金融机构将合并报送相关账户,展示在本报告的借贷交易明细信息中; 此时账户的还款方式为“不区分还款方式”,该账户的还款频率统一约定为“月”,“还款期数”按月计算, 其还款信息按月进行观测和更新。10.本报告中的逾期准贷记卡账户是指该账户 60 天以上的透支行为。11.本报告中的还款期数为“--”是指该账户是非分期还款。12.本报告不展示 5 年前已经结束的违约行为,以及 5 年前的欠税记录、强制执行记录、民事判决记录、行政处罚记录、电信欠费记录。13.机构说明是数据提供机构对具体业务添加的特别说明信息。14.本人声明是信息主体对信用报告中的信息所附注的简要说明,信用主体对本人声明的真实性负责。15.异议标注是征信中心添加的,用于说明信用主体对信用报告中的哪些信息有异议。16.本报告内容涉及个人隐私,查询者应依法使用、妥善保管。因使用不当造成个人信息泄露的,征信中心将不承担相关责任。17.本报告中所有金额(除“有相关还款责任的企业借款”中的金额外)均为人民币金额,参照查询日前一天的汇。18.本报告整合了数据提供机构以信息主体不同证件报送的信息。</Content></RptExplain></ReportMessage>";
 
 
//        System.out.println(xml2Json(a));
        ZxmxGetScoreReqVo zxmxGetScoreReqVo = new ZxmxGetScoreReqVo();
        zxmxGetScoreReqVo.setZxData(a);
        System.out.println(JSONObject.toJSONString(zxmxGetScoreReqVo));
    }
}