【发布时间】:2018-01-15 14:36:27
【问题描述】:
-
我的结果集中有以下数据……以年和月的形式,我想删除多余的年和月以创建 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);
}
}
如果您对如何解决问题有任何建议,我将不胜感激 谢谢
【问题讨论】:
-
如果我们看一下 2015 年元素...它只有 8 个月从 5 开始到 12...这就是我遇到问题的地方。 2016 年和 2017 年有 12 个月,所以我正在执行月份数组大小为 12 的循环,所以它在 2015 年标签中将所有月份向上移动..
-
解决了问题:需要添加这个条件... if(monitoringMonthArray.get(month).equals("01") && monitoringYearArray.get(year).equals("2015") break ;