我的xml
应为
<?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>
我正在使用if else if语句从xml
构建resultset
,它运行正常。
这是我用来构建上述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>";
有没有其他方法可以在不使用嵌套if语句的情况下实现此目的?
答案 0 :(得分:2)
如果我理解正确,你几乎可以用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");
答案 1 :(得分:1)
有没有其他方法可以在不使用嵌套if的情况下实现此目的 语句?
是,请使用switch
声明。在使用switch
之前,请确保该应用程序正在使用Jdk 1.7。