• 解析xml(当节点中有多个子节点)


    概要:解析一个xml,当一个节点中又包含多个子节点如何解析,对比一个节点中不包括其他节点的情况。

    一,xml样例

    <cisReports batNo="查询批次号" unitName="查询单位名称" subOrgan="分支机构名称" queryUserID="查询操作员登录名" queryCount="查询请求数量" receiveTime="查询申请时间,格式YYYYMMDD HH24:mm:ss">
        <!-- 以下为每个报告申请查得情况 1..n -->
        <cisReport reportID="报告ID" buildEndTime="报告生成结束时间,格式YYYY-MM-DD HH24:mm:ss" queryReasonID="查询原因ID,详见数据字典" subReportTypes="查询的收费子报告ID,多个收费子报告ID用逗号分隔" treatResult="对应的收费子报告收费次数,与subReportTypes一一对应,为大于等于0的值的集合,用逗号分隔" subReportTypesShortCaption="报告查询类型" refID="引用ID,为查询申请条件中的引用ID" hasSystemError="有否系统错误,true:有错误,false:无错误" isFrozen="该客户是否被冻结,true:被冻结,false:未被冻结">
                  <!--  个人基本信息 0..1 -->
            <personBaseInfo subReportType="子报告ID" subReportTypeCost="收费子报告ID" treatResult="子报告查询状态,1:查得,2:未查得,3:其他原因未查得" treatErrorCode="treatResult=3时的错误代码,详见数据字典,treatResult!=3时,该属性不存在" errorMessage="treatResult=3时的错误描述信息,treatResult!=3时,该属性的值为空">
                <!-- 内容节点,0..1  -->
                <item>
                    <!--  最新基本信息 1..1 -->
              //lastBaseInfo节点中不包含其他节点 <lastBaseInfo> <name>规范化后的姓名</name> <gender>性别代码:1 男 2女 3 不详</gender> <birthday>出生日期,格式YYYYMMDD</birthday> <marriageStatus>婚姻状况ID</marriageStatus> <registerAddress>规范化的户籍地址</registerAddress> <currentAddress>规范化的现住址</currentAddress> <tel>联系电话</tel> <mobile>移动电话</mobile> <occupationType>职业ID</occupationType> <positionType>职务ID</positionType> <titleType>职称ID</titleType> <education>教育程度id</education> <infoDate>信息获取日期,格式YYYYMMDD,可能为空。</infoDate> </lastBaseInfo> <!-- 身份警示信息 1..1 -->
              //documentAlert节点中又包含多个item子节点 <documentAlert> <!-- 0..n -->//代表可能包含多个item节点 <item> <name>规范化后的姓名</name> <documentNo>规范化后的证件号码</documentNo> <documentType>证件类型ID,详见数据字典</documentType> <country>证件签发地三位英文国际编码,详见数据字典</country> <gender>性别ID,1:男,2:女,3:不详</gender> <birthday>出生日期,格式YYYYMMDD</birthday> <infoDate>信息获取日期,格式YYYYMMDD,可能为空。</infoDate> </item> </documentAlert> </item> </personBaseInfo>

    二,代码

    /**
         * @return com.pingan.credit.model.py.ShenZhenPersonalCredit.PersonBaseInfo
         * @Description: 2. 解析 个人基本信息
         * @date 2017/10/9 17:11
         */
        private PersonBaseInfo getPersonBaseInfoNode(Element cisReport) throws Exception {
            // 获取节点属性
            Element personBaseInfoElement = cisReport.element("personBaseInfo");
            PersonBaseInfo personBaseInfo = null;
            if (personBaseInfoElement != null) {
                List<Attribute> personBaseInfoList = personBaseInfoElement.attributes();
    
                Map<String, String> attributeMap = personBaseInfoList.stream()
                        .collect(Collectors.toMap(Attribute::getName, Attribute::getValue));
                personBaseInfo = (PersonBaseInfo) CommonUtils.setValueOfSuperClass(PersonBaseInfo.class, attributeMap);
    
                //treatResult=1 表示查询状态 查得
                if (SUCCESS.equals(attributeMap.get(TREAT_RESULT))) {
                    Element item = personBaseInfoElement.element("item");
                    if (item != null) {
                        //2 最新基本信息
                //解析单个节点,不包含子节点 Element lastBaseElement = item.element("lastBaseInfo"); if (lastBaseElement != null) { List<Element> lastBaseInfoList = item.element("lastBaseInfo").elements(); Map<String, String> lastBaseInfoListResultMap = XmlUtil.getResultMap(lastBaseInfoList); LastBaseInfo lastBaseInfo = new LastBaseInfo(); personBaseInfo.setLastBaseInfo((LastBaseInfo) CommonUtils.setValue(lastBaseInfo, lastBaseInfoListResultMap)); } //6 身份警示信息
                //节点中包含多个子节点 Element documentAlertElement = item.element("documentAlert"); if (documentAlertElement != null) { List<Element> documentAlertList = item.element("documentAlert").elements(); if (!ListUtil.isEmpty(documentAlertList)) { List<DocumentAlert> itemList = new ArrayList<>();
                     //遍历子节点,获取每个子节点中节点内容
    for (Element e : documentAlertList) { DocumentAlert documentAlert = new DocumentAlert(); documentAlert = (DocumentAlert) CommonUtils.setValue(documentAlert, XmlUtil.getResultMap(e.elements())); itemList.add(documentAlert); } personBaseInfo.setDocumentAlerts(itemList); } } } } } return personBaseInfo; }

    解析节点反射赋值

        /**
         * 将元素节点转换成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;
        }
        /**
         * @param object, resultMap
         * @return java.lang.Object
         * @Description: 通过反射为实体类赋值
         * @date 2017/8/30 9:43
         */
        public static Object setValue(Object object, Map<String, String> resultMap) throws Exception {
            try {
                Class<?> obj = object.getClass();
                Field[] fields = obj.getDeclaredFields();
                setValue(object, resultMap, fields);
    
                return object;
            } catch (IllegalAccessException e) {
                logger.info("IllegalAccessException@CommonUtils:" + e.getMessage());
                throw e;
            } catch (Exception e) {
                logger.info("Exception@CommonUtils:" + e.getMessage());
                throw e;
            }
        }
  • 相关阅读:
    判断字符中是否包含汉字
    since I lived here; since I have lived here. 的区别? 从语法上看, 为啥会有这样的区别?
    have married; have been married; 到底是结婚了没?还是已经离婚了?
    C#项目依据 x86 x64 配置不同的引用
    现在完成时可以表示过去事件对现在的影响/效果. 过去完成时也可以起相同的作用!!!!
    使用现在完成时的常见错误(转发)
    去除win10下的缺省ctrl加空格功能
    appear + 表语 与 appear to be + "表语" 的区别; get hurt与 get to be hurt的区别
    ssm搭建的一个web应用的工作流程
    return和finally究竟谁先执行,还有return是怎么返回数据的
  • 原文地址:https://www.cnblogs.com/shenkebky/p/8334021.html
Copyright © 2020-2023  润新知