I have the following data… in the form of Years and Months in my resultSet and I want to remove redundant years and months to create xml elements as:
[2015, 2016, 2017] [05, 06, 07, 08, 09, 10, 11, 12, 01, 02, 03, 04]
and I am trying to generate XML Tags out of this data Year and Month wise as:
<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>
But the output I am getting is as:
<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>
That is how I am tackling my problem
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);
}
}
If you have any suggestions how to solve the problem, i would greatly appreciate Thanks