Dom4j解析xml复杂多节点报文方式

 更新时间:2021年9月25日 16:00  点击:1595

Dom4j解析xml复杂多节点报文

XML是一种通用的数据交换格式,它的平台无关性、语言无关性、系统无关性、给数据集成与交互带来了极大的方便。XML在不同的语言环境中解析方式都是一样的,只不过实现的语法不同而已。

XML的解析方式分为四种

1、DOM解析

2、SAX解析

3、JDOM解析

4、DOM4J解析

其中前两种属于基础方法,是官方提供的平台无关的解析方式;后两种属于扩展方法,它们是在基础的方法上扩展出来的,只适用于java平台。

本文介绍的是DOM4J方式解析

依赖jar包:

  <dependency>
   <groupId>dom4j</groupId>
   <artifactId>dom4j</artifactId>
   <version>1.6.1</version>
  </dependency>

xml报文如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
    <SttlCntNb>2</SttlCntNb>
    <DebitCntAmt>CNY0.00</DebitCntAmt>
    <CreditCntAmt>CNY4700.00</CreditCntAmt>
    <SttlList>
        <SttlInf>
            <SttlReptFlg>2018052500170139</SttlReptFlg>
            <SttlDCFlg>2</SttlDCFlg>
            <SttlAmt>CNY100.00</SttlAmt>
            <BatchList>
                <BatchInf>
                    <BatchId>B201805230015</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY100.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0113|C3228644000018|04|CNY0.00|0|CNY5.00|1|</SubItemInf>
                        <SubItemInf>0120|C3228640000020|05|CNY50.00|1|CNY0.00|0|</SubItemInf>
                        <SubItemInf>0114|C3228640000029|07|CNY0.00|0|CNY5.00|1|</SubItemInf>
                        <SubItemInf>0115|C3228640000019|06|CNY0.00|0|CNY5.00|1|</SubItemInf>
                        <SubItemInf>0111|C3228640000016|03|CNY0.00|0|CNY10.00|1|</SubItemInf>
                        <SubItemInf>0110|C3228644000016|01|CNY0.00|0|CNY110.00|1|</SubItemInf>
                        <SubItemInf>0112|C3228644000017|99|CNY0.00|0|CNY5.00|1|</SubItemInf>
                        <SubItemInf>0110|C3228644000016|02|CNY0.00|0|CNY10.00|1|</SubItemInf>
                    </SubItemList>
                </BatchInf>
            </BatchList>
        </SttlInf>
        <SttlInf>
            <SttlReptFlg>2018052500170138</SttlReptFlg>
            <SttlDCFlg>2</SttlDCFlg>
            <SttlAmt>CNY4600.00</SttlAmt>
            <BatchList>
                <BatchInf>
                    <BatchId>B201805240001</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY400.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0126|C1010511003703|00|CNY0.00|4|CNY0.00|0|</SubItemInf>
                        <SubItemInf>0125|C1010211000012|01|CNY0.00|8|CNY0.00|0|</SubItemInf>
                        <SubItemInf>0126|C1010211000012|01|CNY0.00|8|CNY0.00|0|</SubItemInf>
                        <SubItemInf>0124|C1010211000012|01|CNY0.00|8|CNY0.00|0|</SubItemInf>
                        <SubItemInf>0123|C1010211000012|01|CNY0.00|4|CNY0.00|0|</SubItemInf>
                        <SubItemInf>0110|C1010211000012|01|CNY0.00|0|CNY400.00|2|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240002</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY400.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0125|C1010211000012|00|CNY400.00|4|CNY0.00|0|</SubItemInf>
                        <SubItemInf>0110|C1010211000012|00|CNY0.00|0|CNY800.00|2|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240003</BatchId>
                    <BatchDCFlg>1</BatchDCFlg>
                    <BatchNetAmt>CNY0.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0120|C1010211000012|00|CNY0.00|4|CNY0.00|0|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240004</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY1200.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0112|C1010211000012|01|CNY0.00|0|CNY1200.00|2|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240005</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY400.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0113|C1010211000012|00|CNY0.00|0|CNY400.00|4|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240006</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY400.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0114|C1010211000012|00|CNY0.00|0|CNY400.00|4|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240007</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY200.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0115|C1010211000012|01|CNY0.00|0|CNY200.00|2|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240008</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY600.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0115|C1010211000012|01|CNY0.00|0|CNY600.00|2|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240009</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY400.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0110|C1010511003703|00|CNY0.00|0|CNY400.00|2|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240010</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY1200.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0111|C1010211000012|01|CNY0.00|0|CNY1200.00|4|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240012</BatchId>
                    <BatchDCFlg>1</BatchDCFlg>
                    <BatchNetAmt>CNY1200.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0116|C1010511003703|00|CNY1200.00|4|CNY0.00|0|</SubItemInf>
                    </SubItemList>
                </BatchInf>
            </BatchList>
        </SttlInf>
    </SttlList>
</Root>

主要核心代码如下

  • String saveFile 为本地保存xml报文的路径。
  • AccountInfoEntityResp 为解析xml后封装的obj实体对象。

/**
     * 解析xml文件并
     */
    public String Dom4jGetXML(String saveFile){
        //返回的Entity对象
        String xmlContents = new String();
        // 创建SAXReader的对象reader
        SAXReader reader = new SAXReader();
        try {
//            Resource resource = new ClassPathResource("accountInfo/"+accountDate+".xml");
            Resource resource = new ClassPathResource(saveFile);
            File file = null;
            try{
                file = resource.getFile();
            }catch (Exception e){
                //抛出异常
            }
            // 通过reader对象的read方法加载books.xml文件,获取docuemnt对象。
            Document document = reader.read(file);
            // 通过document对象获取根节点bookstore
            Element rootElement = document.getRootElement();
            //document转换为String字符串
            xmlContents  = document.asXML();
            //解析xml文件并转换为obj
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return xmlContents;
    }

/**
     * 解析对账文件xml,转换为obj对象返回前段
     * @Descriptions: 循环遍历所有子节点,保存每个节点的值
     * @Return: AccountInfoEntity
     */
    public AccountInfoEntityResp getNodes(Element node){
        AccountInfoEntityResp accountInfoEntityResp = new AccountInfoEntityResp();
        List<SttlInf> SttlList = new ArrayList<SttlInf>();
        //获得指定节点下面的子节点,首先要知道自己要操作的节点。
        Element SttlCntNbElem = node.element("SttlCntNb");
        //获取SttlCntNb
        String SttlCntNb = SttlCntNbElem.getTextTrim();
        //获取DebitCntAmt
        Element DebitCntAmtElem = node.element("DebitCntAmt");
        String DebitCntAmt = DebitCntAmtElem.getTextTrim();
        //获取CreditCntAmt
        Element CreditCntAmtElem = node.element("CreditCntAmt");
        String CreditCntAmt = CreditCntAmtElem.getTextTrim();
        accountInfoEntityResp.setSttlCntNb(Integer.valueOf(SttlCntNb));
        BigDecimal CreditCntAmtVal = null;
        if(CreditCntAmt != null && !CreditCntAmt.isEmpty()){
            CreditCntAmtVal = new BigDecimal(CreditCntAmt.replace("CNY", ""));
        }
        accountInfoEntityResp.setCreditCntAmt(CreditCntAmtVal);
        BigDecimal DebitCntAmtval = null;
        if(DebitCntAmt != null && !DebitCntAmt.isEmpty()){
            DebitCntAmtval = new BigDecimal(DebitCntAmt.replace("CNY", ""));
        }
        accountInfoEntityResp.setDebitCntAmt(DebitCntAmtval);
        //获取SttlList
        Element SttlListEle = node.element("SttlList");
        //获取SttlInf
        Element SttlInfEle = SttlListEle.element("SttlInf");
        List<Element> SttlInfList = SttlListEle.elements();
        //遍历SttlInfList节点
        String SttlReptFlg  = "";
        Integer SttlDCFlg = null;
        BigDecimal SttlAmt = null;
        for(Element e : SttlInfList){
            List<BatchInf> BatchList = new ArrayList<BatchInf>();
            SttlInf sttlInf = new SttlInf();
            //SttlInf下的子节点
            Element SttlReptFlgEle = e.element("SttlReptFlg");
            SttlReptFlg = SttlReptFlgEle.getTextTrim();
            Element SttlDCFlgEle = e.element("SttlDCFlg");
            SttlDCFlg = Integer.valueOf(SttlDCFlgEle.getTextTrim());
            Element SttlAmtEle = e.element("SttlAmt");
            SttlAmt = new BigDecimal(SttlAmtEle.getTextTrim().replace("CNY", ""));
            //设值
            sttlInf.setSttlAmt(SttlAmt);
            sttlInf.setSttlDCFlg(SttlDCFlg);
            sttlInf.setSttlReptFlg(SttlReptFlg);
            //获取BatchList节点
            Element BatchListEle = e.element("BatchList");
            //获取BatchInf
            List<Element> BatchInfList = BatchListEle.elements();
            for(Element e2 : BatchInfList){
                List<SubItemInf> SubItemList = new ArrayList<SubItemInf>();
                BatchInf batchInf = new BatchInf();
                String BatchId = "";
                Integer BatchDCFlg = null;
                BigDecimal BatchNetAmt = null;
                Element BatchIdEle = e2.element("BatchId");
                BatchId = BatchIdEle.getTextTrim();
                Element BatchDCFlgEle = e2.element("BatchDCFlg");
                BatchDCFlg = Integer.valueOf(BatchDCFlgEle.getTextTrim());
                Element BatchNetAmtEle = e2.element("BatchNetAmt");
                BatchNetAmt = new BigDecimal(BatchNetAmtEle.getTextTrim().replace("CNY", ""));
                batchInf.setBatchDCFlg(BatchDCFlg);
                batchInf.setBatchId(BatchId);
                batchInf.setBatchNetAmt(BatchNetAmt);
                //获取SubItemList节点
                Element SubItemListEle = e2.element("SubItemList");
                //获取BatchInf
                List<Element> SubItemInfList = SubItemListEle.elements();
                for(Element e3 : SubItemInfList){
                    SubItemInf subItemInf = new SubItemInf();
                    String subItem = "";
                    subItem = e3.getTextTrim().replace("CNY", "");
                    subItemInf.setSubItemInf(subItem);
                    SubItemList.add(subItemInf);
                }
                batchInf.setSubItemList(SubItemList);
                BatchList.add(batchInf);
            }
            sttlInf.setBatchList(BatchList);
            SttlList.add(sttlInf);
        }
        accountInfoEntityResp.setSttlList(SttlList);
        return accountInfoEntityResp;
    }

dom4j解析多层xml

package com.bessky.hrmis.test;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Dom4JDemo {
    public static void main(String[] args) throws Exception {
        // 1.创建Reader对象
        SAXReader reader = new SAXReader();
        String str = "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>\r\n"
            + "    <cas:authenticationSuccess>\r\n" + "        <cas:user>hanke_tom</cas:user>\r\n"
            + "        <cas:attributes>\r\n"
            + "            <cas:credentialType>tom</cas:credentialType>\r\n"
            + "            <cas:logout_url>http://192.168.1.5:8080/sso/logout</cas:logout_url>\r\n"
            + "            <cas:isFromNewLogin>false</cas:isFromNewLogin>\r\n"
            + "            <cas:authenticationDate>2019-11-26T17:47:55.789+08:00[Asia/Shanghai]</cas:authenticationDate>\r\n"
            + "            <cas:user_id>01</cas:user_id>\r\n"
            + "            <cas:authenticationMethod>TomAuthenticationHandler</cas:authenticationMethod>\r\n"
            + "            <cas:successfulAuthenticationHandlers>TomAuthenticationHandler</cas:successfulAuthenticationHandlers>\r\n"
            + "            <cas:name>张三</cas:name>\r\n"
            + "            <cas:longTermAuthenticationRequestTokenUsed>false</cas:longTermAuthenticationRequestTokenUsed>\r\n"
            + "            <cas:job_number>00001</cas:job_number>\r\n"
            + "            <cas:username>hanke_tom</cas:username>\r\n" + "            </cas:attributes>\r\n"
            + "    </cas:authenticationSuccess>\r\n" + "</cas:serviceResponse>";
        // 2.加载xml
        Document document = DocumentHelper.parseText(str);
        // 3.获取根节点
        Element rootElement = document.getRootElement();
        Iterator iterator = rootElement.elementIterator();
        
        System.out.println("根节点:" + rootElement.getName()); // 拿到根节点的名称
        
        Iterator bbbb = rootElement.elementIterator("authenticationSuccess"); 
        
        while (bbbb.hasNext()) {
            Element recordEless = (Element) bbbb.next();
            Iterator cccc = recordEless.elementIterator("attributes"); 
            while (cccc.hasNext()) {
                Element itemEle = (Element) cccc.next();
                String username = itemEle.elementTextTrim("job_number"); 
                System.err.println("---------"+username);
            }
        }
    }
}

如果后面还有子级继续while向下取数据

以上为个人经验,希望能给大家一个参考,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • 解决Mybatis中mapper.xml文件update,delete及insert返回值问题

    这篇文章主要介绍了解决Mybatis中mapper.xml文件update,delete及insert返回值问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-23
  • C#从数据库读取数据到DataSet并保存到xml文件的方法

    这篇文章主要介绍了C#从数据库读取数据到DataSet并保存到xml文件的方法,涉及C#操作DataSet保存到XML文件的技巧,需要的朋友可以参考下...2020-06-25
  • XML、HTML、CSS与JS的区别整理

    在BS中,xml,html,css和js我们都学过,起初分不清这四者的区别和联系,随着知识的增长,有了一些体会,下面通过本文给大家简单介绍 XML、HTML、CSS与JS的区别,需要的朋友参考下...2016-02-21
  • js操作XML文件的实现方法兼容IE与FireFox

    下面小编就为大家带来一篇js操作XML文件的实现方法兼容IE与FireFox。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-07-01
  • Xml中使用foreach遍历对象实现代码

    这篇文章主要介绍了Xml中使用foreach遍历对象实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-12-04
  • JS跨浏览器解析XML应用过程详解

    这篇文章主要介绍了JS跨浏览器解析XML应用过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-10-16
  • Python将字典转换为XML的方法

    这篇文章主要介绍了Python将字典转换为XML的方法,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-08-02
  • Python xmltodict模块安装及代码实例

    这篇文章主要介绍了Python xmltodict模块安装及代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-10-05
  • 基于C#中XmlReader读取Xml的深入分析

    本篇文章是对C#中XmlReader读取Xml进行了详细的分析介绍,需要的朋友参考下...2020-06-25
  • 使用maven开发springboot项目时pom.xml常用配置(推荐)

    这篇文章主要介绍了使用maven开发springboot项目时的pom.xml常用配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-01-08
  • C# 对XML基本操作代码总结

    C# 对XML基本操作包括读取节点的数据,添加节点。读取节点属性,修改节点属性等...2020-06-25
  • asp.net中XML如何做增删改查操作

    工作之余,温习一下对xml的简单操作,抽个时间把asp.net XML增删改查操作方法分享给大家,需要的朋友可以参考下...2020-06-25
  • C#实现复杂XML的序列化与反序列化

    这篇文章主要介绍了C#实现复杂XML的序列化与反序列化的方法,是非常实用的一个技巧,需要的朋友可以参考下...2020-06-25
  • 使用xmltextreader对象读取xml文档示例

    这篇文章主要介绍了使用xmltextreader对象读取xml文档的示例,需要的朋友可以参考下...2020-06-25
  • 深入分析XmlSerializer对象的Xml序列化与反序列化的示例详解

    本篇文章是对XmlSerializer 对象的Xml序列化与反序列化的应用进行了详细的分析介绍,需要的朋友参考下...2021-09-22
  • Mybatis或Mybatis-Plus框架的xml文件中特殊符号的使用详解

    这篇文章主要介绍了Mybatis或Mybatis-Plus框架的xml文件中特殊符号的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-19
  • 详解C#对XML、JSON等格式的解析

    这篇文章主要介绍了详解C#对XML、JSON等格式的解析,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。...2020-06-25
  • 这一次搞懂Spring的XML解析原理说明

    这篇文章主要介绍了这一次搞懂Spring的XML解析原理说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-27
  • Java如何基于DOM解析xml文件

    这篇文章主要介绍了Java如何基于DOM解析xml文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-09-16
  • php中SimpleXMLElement 对象转换为数组

    PHP 提供了 simplexml_load_string 方法用来解析 XML 格式的字符串,并返回 SimpleXMLElement 对象。不过一般数组是更为适用的,所以也会有转换为普通数组的需求,这个方法...2016-11-25