我在resultSet中以年和月的形式提供以下数据...我想删除多余的年份和月份来创建xml元素:
[2015,2016,2017] [05,06,07,08,09,10,11,12,01,02,03,04]
我试图从年度和月份的数据中生成XML标签:
<Root>
<Year Y="2015">
<Month M="05"/>
<Month M="06"/>
<Month M="07"/>
<Month M="08"/>
<Month M="09"/>
<Month M="10"/>
<Month M="11"/>
<Month M="12"/>
</Year>
<Year Y="2016">
<Month M="01"/>
<Month M="02"/>
<Month M="03"/>
<Month M="04"/>
<Month M="05"/>
<Month M="06"/>
<Month M="07"/>
<Month M="08"/>
<Month M="09"/>
<Month M="10"/>
<Month M="11"/>
<Month M="12"/>
</Year>
<Year Y="2017">
<Month M="01"/>
<Month M="02"/>
<Month M="03"/>
<Month M="04"/>
<Month M="05"/>
<Month M="06"/>
<Month M="07"/>
<Month M="08"/>
<Month M="09"/>
<Month M="10"/>
<Month M="11"/>
<Month M="12"/>
</Year>
</Root>
但我得到的输出是:
<Root>
<Year Y="2015">
<Month M="05"/>
<Month M="06"/>
<Month M="07"/>
<Month M="08"/>
<Month M="09"/>
<Month M="10"/>
<Month M="11"/>
<Month M="12"/>
<Month M="01"/>
<Month M="02"/>
<Month M="03"/>
<Month M="04"/>
</Year>
<Year Y="2016">
<Month M="05"/>
<Month M="06"/>
<Month M="07"/>
<Month M="08"/>
<Month M="09"/>
<Month M="10"/>
<Month M="11"/>
<Month M="12"/>
<Month M="01"/>
<Month M="02"/>
<Month M="03"/>
<Month M="04"/>
</Year>
<Year Y="2017">
<Month M="05"/>
<Month M="06"/>
<Month M="07"/>
<Month M="08"/>
<Month M="09"/>
<Month M="10"/>
<Month M="11"/>
<Month M="12"/>
<Month M="01"/>
<Month M="02"/>
<Month M="03"/>
<Month M="04"/>
</Year>
</Root>
这就是我解决问题的方法
try{
//Connection
connection = DriverManager.getConnection(conUrl);
statement = connection.createStatement();
resultSet = statement.executeQuery(query);
//XML
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
// Super Root Node (Necessity)
Element necessaryElement = document.createElement("Root");
document.appendChild(necessaryElement);
while(resultSet.next()){
//Year Array populating
monitoringYear = resultSet.getString("Year");
monitoringYearArray.add(monitoringYear);
//Month Array populating
monitoringMonth = resultSet.getString("Month");
System.out.println( "Monitoring Month "+ monitoringMonth);
monitoringMonthArray.add(monitoringMonth);
}
// remove duplicate from the Years ArrayList and retain the order
Set<String> uniqueYearList = new LinkedHashSet<String>(monitoringYearArray);
monitoringYearArray.clear();
monitoringYearArray.addAll(uniqueYearList);
// Remove duplicate from the Month ArrayList and retain the order
Set<String> uniqueMonthList = new LinkedHashSet<String>(monitoringMonthArray);
monitoringMonthArray.clear();
monitoringMonthArray.addAll(uniqueMonthList);
// XML Element for the Year
for(int year = 0; year < monitoringYearArray.size(); year++){
// Super Root Node (Year)
Element yearElement = document.createElement("Year");
necessaryElement.appendChild(yearElement);
Attr yearAttr = document.createAttribute("Y");
yearAttr.setValue(monitoringYearArray.get(year));
yearElement.setAttributeNode(yearAttr);
// XML Element for the Month
for(int month = 0; month < monitoringMonthArray.size(); month++){
// Sub Root Node (Month)
Element monthElement = document.createElement("Month");
yearElement.appendChild(monthElement);
Attr monthAttr = document.createAttribute("M");
monthAttr.setValue(monitoringMonthArray.get(month));
monthElement.setAttributeNode(monthAttr);
}
}
如果您有任何建议如何解决问题,我将不胜感激 感谢
答案 0 :(得分:0)
我认为使用Set Object和while循环在一个级别覆盖月份如果你想保留所有值的密钥,你可以考虑实现像apache.commons.collections.MultiHashMap或< / p>
HashMap<String, LinkedHashSet> monthsincludedmap = new HashMap<String, LinkedHashSet >();