title: how to group xml elements in java by attributes
body: <p>I have a requirement for grouping XML elements with same attribute in Java.</p>
  
  <p>Below is my updated input:</p>
  
  <pre><code>&lt;root&gt;
   &lt;Slots date="2015-11-17"&gt;
      &lt;TimePeriod value="8-17"&gt;
          &lt;ContractorAvailable ContractorID="H4CONT07"&gt;
              &lt;GroupId&gt;298451&lt;/GroupId&gt;
              &lt;OfferToken&gt;10315009&lt;/OfferToken&gt;
              &lt;Capacity&gt;99&lt;/Capacity&gt;
          &lt;/ContractorAvailable&gt;
      &lt;/TimePeriod&gt;
  &lt;/Slots&gt;
  &lt;Slots date="2015-11-17"&gt;
      &lt;TimePeriod value="8-17"&gt;
          &lt;ContractorAvailable ContractorID="H4CONT07"&gt;
              &lt;GroupId&gt;298452&lt;/GroupId&gt;
              &lt;OfferToken&gt;10315009&lt;/OfferToken&gt;
              &lt;Capacity&gt;99&lt;/Capacity&gt;
          &lt;/ContractorAvailable&gt;
      &lt;/TimePeriod&gt;
  &lt;/Slots&gt;
   &lt;Slots date="2015-11-18"&gt;
      &lt;TimePeriod value="2-8"&gt;
          &lt;ContractorAvailable ContractorID="H4CONT07"&gt;
              &lt;GroupId&gt;298451&lt;/GroupId&gt;
              &lt;OfferToken&gt;10315009&lt;/OfferToken&gt;
              &lt;Capacity&gt;99&lt;/Capacity&gt;
          &lt;/ContractorAvailable&gt;
      &lt;/TimePeriod&gt;
  &lt;/Slots&gt;
  &lt;Slots date="2015-11-18"&gt;
      &lt;TimePeriod value="2-8"&gt;
          &lt;ContractorAvailable ContractorID="H4CONT07"&gt;
              &lt;GroupId&gt;298452&lt;/GroupId&gt;
              &lt;OfferToken&gt;10315009&lt;/OfferToken&gt;
              &lt;Capacity&gt;99&lt;/Capacity&gt;
          &lt;/ContractorAvailable&gt;
      &lt;/TimePeriod&gt;
  &lt;/Slots&gt;
  </code></pre>
  
  <p></p>
  
  <p>I need the output as below:</p>
  
  <pre><code> &lt;root&gt;
   &lt;Slots date="2015-11-17"&gt;
      &lt;TimePeriod value="8-17"&gt;
          &lt;ContractorAvailable ContractorID="H4CONT07"&gt;
              &lt;GroupId&gt;298451&lt;/GroupId&gt;
              &lt;OfferToken&gt;10315009&lt;/OfferToken&gt;
              &lt;Capacity&gt;99&lt;/Capacity&gt;
          &lt;/ContractorAvailable&gt;
          &lt;ContractorAvailable ContractorID="H4CONT07"&gt;
              &lt;GroupId&gt;298452&lt;/GroupId&gt;
              &lt;OfferToken&gt;10315009&lt;/OfferToken&gt;
              &lt;Capacity&gt;99&lt;/Capacity&gt;
          &lt;/ContractorAvailable&gt;
      &lt;/TimePeriod&gt;
   &lt;/Slots&gt;
   &lt;Slots date="2015-11-18"&gt;
      &lt;TimePeriod value="2-8"&gt;
          &lt;ContractorAvailable ContractorID="H4CONT07"&gt;
              &lt;GroupId&gt;298451&lt;/GroupId&gt;
              &lt;OfferToken&gt;10315009&lt;/OfferToken&gt;
              &lt;Capacity&gt;99&lt;/Capacity&gt;
          &lt;/ContractorAvailable&gt;
          &lt;ContractorAvailable ContractorID="H4CONT07"&gt;
              &lt;GroupId&gt;298452&lt;/GroupId&gt;
              &lt;OfferToken&gt;10315009&lt;/OfferToken&gt;
              &lt;Capacity&gt;99&lt;/Capacity&gt;
          &lt;/ContractorAvailable&gt;
      &lt;/TimePeriod&gt;
  &lt;/Slots&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>I tried something like the below:</p>
  
  <pre><code>HashSet&lt;String&gt; set = new HashSet&lt;String&gt;();
       if( node!= null &amp;&amp; node.getLength() &gt; 0)  {
  
  
           for(int i=0;i&lt;node.getLength();i=i+1)
           {  
                 Node slots = node.item(i);
                 set.add(slots.getAttributes().getNamedItem("date").getNodeValue());
           } 
      }
       System.out.println("slots dates are  --- **** "+ set);
       ArrayList&lt;Node&gt; deleteList = new ArrayList&lt;Node&gt;();
      for(String date  : set)
       {  System.out.println("slots dates is --- **** "+ "2015-11-18");
             Node remainingNode = null;
             for(int i=0;i&lt;node.getLength();i=i+1)
             {  
                   Node slotnode = node.item(i);
                   if( slotnode.getAttributes() != null &amp;&amp; slotnode.getAttributes().getNamedItem("date")!= null &amp;&amp; "2015-11-18".equals(slotnode.getAttributes().getNamedItem("date").getNodeValue()))
                   {System.out.println("inside time 18");
                         if(remainingNode == null)
                               remainingNode = slotnode;
                         else {
                               NodeList nodeps = slotnode.getChildNodes();
                               for(int j=0;j&lt;nodeps.getLength();j=i+1) {
                                     remainingNode.appendChild(nodeps.item(j));
                               }
                               deleteList.add(slotnode);
                         }
                   }
             }  
             NodeList nodesItems = doc1.getElementsByTagName("root"); 
             nodesItems.item(0).appendChild(remainingNode);
  </code></pre>
  
  <p>I tried with using Dom parser, but I was not able to do. I am new to XML, searched everywhere but didn't find anything, Can anyone please help me in this.
  Note:I can't use JAXB for this solution.</p>
  
  <p>yes i have group Slots by date and then additionally by TimePeriod value.
  XSLT is not supported by tool which i am using</p>