Multiple nested for loops to generate xml, need to obtain two variables, how to simplify

write a loop to generate xml, should be able to use recursive simplification, but there is no idea when writing, because you need to pass two variables

    //
    Document root = DocumentHelper.createDocument();
    //
    Element CNCRS = root.addElement("cncrs:CNCRS");
    //
    CNCRS.addNamespace("stc", "http://aeoi.chinatax.gov.cn/crs/stctypes/v1");
    CNCRS.addNamespace("cncrs", "http://aeoi.chinatax.gov.cn/crs/cncrs/v1");
    //
    CNCRS.addAttribute("verison", "1.0");
    String data = "hhhh";
    //
    String sql = "select * from crs_table where CRS_LEVEL="1"";
    List<?> list = dao.queryWithJDBC(sql);
    for(int i = 0;i<list.size();iPP){
     Map<String, String> map = (Map<String, String>) list.get(i);
     //ID
     String id = map.get("CRS_ID");
     System.out.println(id);
     //
     String element = map.get("CRS_ELEMENT");
     //
     String parent_element = map.get("CRS_PARENT_ELEMENT");
     //
     String sql1 = "select crs_value from crs_value where CRS_ELEMENT = ""+element+"" and CRS_LEVEL="1"";
     Map<String, String> map2 = (Map<String, String>) dao.queryWithJDBC(sql1).get(0);
     //
     Element oneLevel = CNCRS.addElement(element);
     //
     if (map2.get("CRS_value") != null) {
         oneLevel.setText(map2.get("CRS_value"));
    }
     sql = "select * from crs_table where CRS_LEVEL="2" and CRS_PARENT_ID = ""+id+""";
     List<?> list1 = dao.queryWithJDBC(sql);
         for (int a = 0; a<list1.size(); aPP) {
              map = (Map<String, String>) list1.get(a);
             id = map.get("CRS_ID");
             System.out.println(id);
             element = map.get("CRS_ELEMENT");
             parent_element = map.get("CRS_PARENT_ELEMENT");
             String sql2 = "select crs_value from crs_value where CRS_ELEMENT = ""+element+"" and CRS_PARENT_ELEMENT = ""+parent_element+"" and CRS_LEVEL="2"";
             map2 = (Map<String, String>) dao.queryWithJDBC(sql2).get(0);
             Element twoLevel = oneLevel.addElement(element);
             if (map2.get("CRS_value") != null) {
                 twoLevel.setText(map2.get("CRS_value"));
            }
             sql = "select * from crs_table where CRS_LEVEL="3" and CRS_PARENT_ID = ""+id+""";
             List<?> list2 = dao.queryWithJDBC(sql);
                 for (int b = 0; b<list2.size(); bPP) {
                      map = (Map<String, String>) list2.get(b);
                     id = map.get("CRS_ID");
                     System.out.println(id);
                     element = map.get("CRS_ELEMENT");
                     parent_element = map.get("CRS_PARENT_ELEMENT");
                     String sql3 = "select crs_value from crs_value where CRS_ELEMENT = ""+element+"" and CRS_PARENT_ELEMENT = ""+parent_element+"" and CRS_LEVEL="3"";
                     map2 = (Map<String, String>) dao.queryWithJDBC(sql3).get(0);
                     Element thrLevel = twoLevel.addElement(element);
                     if (map2.get("CRS_value") != null) {
                         thrLevel.setText(map2.get("CRS_value"));
                    }
                     if (("cncrs:AccountBalance").equals(element)) {
                         thrLevel.addAttribute("currCode", "USD");
                    }
                     sql = "select * from crs_table where CRS_LEVEL="4" and CRS_PARENT_ID = ""+id+""";
                     List<?> list3 = dao.queryWithJDBC(sql);
                         for (int c = 0; c<list3.size(); cPP) {
                              map = (Map<String, String>) list3.get(c);
                             id = map.get("CRS_ID");
                             System.out.println(id);
                             element = map.get("CRS_ELEMENT");
                             parent_element = map.get("CRS_PARENT_ELEMENT");
                             String sql4 = "select crs_value from crs_value where CRS_ELEMENT = ""+element+"" and CRS_PARENT_ELEMENT = ""+parent_element+"" and CRS_LEVEL="4"";
                             map2 = (Map<String, String>) dao.queryWithJDBC(sql4).get(0);
                             Element fourLevel = thrLevel.addElement(element);
                             if (map2.get("CRS_value") != null) {
                                 fourLevel.setText(map2.get("CRS_value"));
                            }
                             if (("cncrs:AccountBalance").equals(element)) {
                                 fourLevel.addAttribute("currCode", "USD");
                            }else if (("stc:Name".equals(element))) {
                                fourLevel.addAttribute("nameType", "OECD202");
                            }else if (("stc:Address".equals(element))) {
                                fourLevel.addAttribute("legalAddressType", "OECD301");
                            }
                             sql = "select * from crs_table where CRS_LEVEL="5" and CRS_PARENT_ID = ""+id+""";
                             List<?> list4 = dao.queryWithJDBC(sql);
                                 for (int d = 0; d<list4.size(); dPP) {
                                      map = (Map<String, String>) list4.get(d);
                                     id = map.get("CRS_ID");
                                     System.out.println(id);
                                     element = map.get("CRS_ELEMENT");
                                     parent_element = map.get("CRS_PARENT_ELEMENT");
                                     String sql5 = "select crs_value from crs_value where CRS_ELEMENT = ""+element+"" and CRS_PARENT_ELEMENT = ""+parent_element+"" and CRS_LEVEL="5"";
                                     map2 = (Map<String, String>) dao.queryWithJDBC(sql5).get(0);
                                     Element fiveLevel = fourLevel.addElement(element);
                                     if (map2.get("CRS_value") != null) {
                                         fiveLevel.setText(map2.get("CRS_value"));
                                    }
                                     if (("stc:Address".equals(element))&&("cncrs:Individual").equals(parent_element)) {
                                         fiveLevel.addAttribute("legalAddressType", "OECD301");
                                        }else if (("stc:Address".equals(element))&&("cncrs:Organisation").equals(parent_element)) {
                                            fiveLevel.addAttribute("legalAddressType", "OECD301");
                                        }else if (("stc:Name".equals(element))&&("cncrs:Individual").equals(parent_element)) {
                                            fiveLevel.addAttribute("nameType", "OECD202");
                                        }else if (("stc:Name".equals(element))&&("cncrs:Organisation").equals(parent_element)) {
                                            fiveLevel.addAttribute("nameType", "OECD207");
                                        }else if (("stc:IN".equals(element))) {
                                            fiveLevel.addAttribute("issuedBy", " ");
                                            fiveLevel.addAttribute("inType", "TIN");
                                        }
                                     
                                     sql = "select * from crs_table where CRS_LEVEL="6" and CRS_PARENT_ID = ""+id+""";
                                     List<?> list5 = dao.queryWithJDBC(sql);
                                         for (int e = 0; e<list5.size(); ePP) {
                                              map = (Map<String, String>) list5.get(e);
                                             id = map.get("CRS_ID");
                                             System.out.println(id);
                                             element = map.get("CRS_ELEMENT");
                                             parent_element = map.get("CRS_PARENT_ELEMENT");
                                             String sql6 = "select crs_value from crs_value where CRS_ELEMENT = ""+element+"" and CRS_PARENT_ELEMENT = ""+parent_element+"" and CRS_LEVEL="6"";
                                             map2 = (Map<String, String>) dao.queryWithJDBC(sql6).get(0);
                                             Element sixLevel = fiveLevel.addElement(element);
                                             if (map2.get("CRS_value") != null) {
                                                 sixLevel.setText(map2.get("CRS_value"));
                                            }
                                             if (("stc:IN".equals(element))) {
                                                 sixLevel.addAttribute("issuedBy", " ");
                                                 sixLevel.addAttribute("inType", "TIN");
                                                }
                                             sql = "select * from crs_table where CRS_LEVEL="7" and CRS_PARENT_ID = ""+id+""";
                                             List<?> list6 = dao.queryWithJDBC(sql);
                                                 for (int f = 0; f<list6.size(); fPP) {
                                                      map = (Map<String, String>) list6.get(f);
                                                     id = map.get("CRS_ID");
                                                     System.out.println(id);
                                                     element = map.get("CRS_ELEMENT");
                                                     parent_element = map.get("CRS_PARENT_ELEMENT");
                                                     String sql7 = "select crs_value from crs_value where CRS_ELEMENT = ""+element+"" and CRS_PARENT_ELEMENT = ""+parent_element+"" and CRS_LEVEL="7"";
                                                     map2 = (Map<String, String>) dao.queryWithJDBC(sql7).get(0);
                                                     Element sevenLevel = sixLevel.addElement(element);
                                                     if (map2.get("CRS_value") != null) {
                                                         sevenLevel.setText(map2.get("CRS_value"));
                                                    }
                                                     sql = "select * from crs_table where CRS_LEVEL="8" and CRS_PARENT_ID = ""+id+""";
                                                     List<?> list7 = dao.queryWithJDBC(sql);
                                                         for (int g = 0; g<list7.size(); gPP) {
                                                              map = (Map<String, String>) list7.get(g);
                                                             id = map.get("CRS_ID");
                                                             System.out.println(id);
                                                             element = map.get("CRS_ELEMENT");
                                                             parent_element = map.get("CRS_PARENT_ELEMENT");
                                                             String sql8 = "select crs_value from crs_value where CRS_ELEMENT = ""+element+"" and CRS_PARENT_ELEMENT = ""+parent_element+"" and CRS_LEVEL="8"";
                                                             map2 = (Map<String, String>) dao.queryWithJDBC(sql8).get(0);
                                                             Element eightLevel = sevenLevel.addElement(element);
                                                             if (map2.get("CRS_value") != null) {
                                                                 eightLevel.setText(map2.get("CRS_value"));
                                                            }
                                                             
                                                         }
                                                 }
                                         }
                                 }
                         }
                 }
         }
     }
Jun.15,2021

it's not a matter of replacing loops with recursion.
is a problem that requires drawing methods and adding types

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b380ce-2c0fc.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b380ce-2c0fc.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?