亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Dom4j如何解析xml復雜多節點報文

發布時間:2021-09-26 09:50:24 來源:億速云 閱讀:128 作者:小新 欄目:開發技術

這篇文章主要介紹了Dom4j如何解析xml復雜多節點報文,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

    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向下取數據

    感謝你能夠認真閱讀完這篇文章,希望小編分享的“Dom4j如何解析xml復雜多節點報文”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!

    向AI問一下細節

    免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

    AI

    益阳市| 肇源县| 江北区| 博湖县| 西乌珠穆沁旗| 泰和县| 阿尔山市| 涿州市| 崇义县| 湘潭县| 方山县| 枝江市| 彰化市| 葫芦岛市| 古丈县| 正定县| 岱山县| 尼玛县| 寻甸| 应用必备| 河源市| 汝阳县| 诸暨市| 永吉县| 柏乡县| 新源县| 保靖县| 庄河市| 杭锦后旗| 晋宁县| 上饶市| 秭归县| 长汀县| 怀柔区| 吉木乃县| 故城县| 砚山县| 抚宁县| 碌曲县| 绥棱县| 合作市|