0
  1. 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

2
  • if we look at the year 2015 Element... it has only 8 months starting from 5 and goes up to 12... that is where I am getting the problem. 2016 and 2017 has 12 months so i have am executing the loop for the size of month array which is 12 so it moves all the months up in the 2015 Tag.. Commented Aug 8, 2017 at 7:05
  • Solved the Problem: needed to add this condition... if(monitoringMonthArray.get(month).equals("01") && monitoringYearArray.get(year).equals("2015") break; Commented Aug 8, 2017 at 8:52

1 Answer 1

0

I think it is use of Set Object and while loop in one level that overwrites the months If you'd like to keep all the values a key is given, you might consider implementing like apache.commons.collections.MultiHashMap or

HashMap<String, LinkedHashSet> monthsincludedmap = new HashMap<String, LinkedHashSet >();

check Difference between HashSet and HashMap?

What happens when a duplicate key is put into a HashMap?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.