Q:

How to avoid nested if statements in java?

My xml should be
<?xml version="1.0" encoding="UTF-8"?>
<Events>
    <sunday><Event><ID>123</ID><Desc>abc</Desc></Event><Event><ID>124</ID><Desc>ade</Desc></Event></sunday>
    <monday><Event><ID>125</ID><Desc>ades</Desc></Event></monday>
    <tuesday><Event><ID>126</ID><Desc>adde</Desc></Event><Event><ID>127</ID><Desc>aade</Desc></Event></tuesday>
    <wednesday><Event><ID>128</ID><Desc>afde</Desc></Event></wednesday>
    <thursday></thursday>
    <friday></friday>
    <saturday></saturday>
</Events>

I am constructing xml from the resultset using the if else if statement and it is working fine.
This is the java code I am using to construct the above xml 
String sunTag = "", monTag = "", tueTag = "", wedTag = "", thuTag = "", friTag = "", satTag = "";

   while (resultSet.next()) {
       if("sunday".equalsIgnoreCase(resultSet.getString("WeekName"))) {                
           sunTag = sunTag + "<Event>";
           sunTag = sunTag + "<ID>" + resultSet.getString("EventID") + "</ID>";
           sunTag = sunTag + "<Desc>" + resultSet.getString("EventDesc") + "</D>";
           sunTag = sunTag + "</Event>";

       } else if("monday".equalsIgnoreCase(resultSet.getString("WeekName"))) {                 
           monTag = monTag + "<Event>";
           monTag = monTag + "<ID>" + resultSet.getString("EventID") + "</ID>";
           monTag = monTag + "<Desc>" + resultSet.getString("EventDesc") + "</D>";
           monTag = monTag + "</Event>";

       } else if("tuesday".equalsIgnoreCase(resultSet.getString("WeekName"))) {                
           tueTag = tueTag + "<Event>";
           tueTag = tueTag + "<ID>" + resultSet.getString("EventID") + "</ID>";
           tueTag = tueTag + "<Desc>" + resultSet.getString("EventDesc") + "</D>";
           tueTag = tueTag + "</Event>";

       } else if("wednesday".equalsIgnoreCase(resultSet.getString("WeekName"))) {                  
           wedTag = wedTag + "<Event>";
           wedTag = wedTag + "<ID>" + resultSet.getString("EventID") + "</ID>";
           wedTag = wedTag + "<D>" + resultSet.getString("EventDesc") + "</D>";
           wedTag = wedTag + "</Event>";

       } else if("thursday".equalsIgnoreCase(resultSet.getString("WeekName"))) {                   
           thuTag = thuTag + "<Event>";
           thuTag = thuTag + "<ID>" + resultSet.getString("EventID") + "</ID>";
           thuTag = thuTag + "<Desc>" + resultSet.getString("EventDesc") + "</D>";
           thuTag = thuTag + "</Event>";

       } else if("friday".equalsIgnoreCase(resultSet.getString("WeekName"))) {                 
           friTag = friTag + "<Event>";
           friTag = friTag + "<ID>" + resultSet.getString("EventID") + "</ID>";
           friTag = friTag + "<Desc>" + resultSet.getString("EventDesc") + "</D>";
           friTag = friTag + "</Event>";

       } else if("saturday".equalsIgnoreCase(resultSet.getString("WeekName"))) {                   
           satTag = satTag + "<Event>";
           satTag = satTag + "<ID>" + resultSet.getString("EventID") + "</ID>";
           satTag = satTag + "<Desc>" + resultSet.getString("EventDesc") + "</D>";
           satTag = satTag + "</Event>";

       }                       
   }                              
   result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
   + <Events>"
        + "<sunday>" + sunTag + "</sunday>" 
        + "<monday>"  + monTag + "</monday>"
        + "<tuesday>" + tueTag + "</tuesday>"
        + "<wednesday>"  + wedTag + "</wednesday>"
        + "<thursday>" + thuTag + "</thursday>"
        + "<friday>"  + friTag + "</friday>"
        + "<saturday>" + satTag + "</saturday>"
    + "</Events>";

Is there any other way to achieve this without using nested if statements?      

A:

If I'm understanding correctly, you can condense it almost entirely with a Map.
Map<String, String> map =
    new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

while(resultSet.next()) {
    String weekName = resultSet.getString("WeekName");
    String tag = map.get(weekName);
    if(tag == null) {
        tag = "";
    }

    tag = tag + "<Event>";
    tag = tag + "<ID>" + resultSet.getString("EventID") + "</ID>";
    tag = tag + "<Desc>" + resultSet.getString("EventDesc") + "</D>";
    tag = tag + "</Event>";

    map.put(weekName, tag);
}

String sunTag = map.get("sunday");
String monTag = map.get("monday");
String tueTag = map.get("tuesday");
String wedTag = map.get("wednesday");
String thuTag = map.get("thursday");
String friTag = map.get("friday");
String satTag = map.get("saturday");

See also The Map Interface.

