0

I'm trying to sort nodes in an xml file, each node contains a little over 300 fields, and so my question is, is there a way to sort these nodes other than the examples I've seen for nodes with only a few fields? like in the following example: sort the xml elements in java

In my case this would be impractical, but if there is no way around it I would have to do it this way.

I would like to sort it by ORDER_LINE

<HEADER>
  <CARTON>
  </CARTON>

<CARTONDETAIL>

<WH>WHS01</WH>

<OWN>ClientID</OWN>

<ORD>1035178</ORD>

<RELEASE_NUM>1</RELEASE_NUM>

<ORDER_LINE>11</ORDER_LINE>

<TRUCK>0242009025</TRUCK>

<ORDERING>3</ORDERING>

<TRAILER_ID/>
300 more element.....
 </CARTONDETAIL>
  <CARTONDETAIL>
   Elements
  </CARTONDETAIL>

 </HEADER>

Thanks for any help

Community
  • 1
  • 1
Sergio B
  • 734
  • 1
  • 5
  • 17

1 Answers1

0

Remove all the nodes you want to sort, stuff them in a List, do Collections.sort() with a custom Comparator that looks into the nodes to extract the ORDER_LINE, then add the sorted list back into the tree.

If you want an over-engineered elegant solution, write an Adapter that turns the ~root element into a List, then hand it off to Collections.sort().

David Ehrmann
  • 7,366
  • 2
  • 31
  • 40
  • That sounds exactly like what I'm looking for, (not the over-engineered) solution. If possible, please share some sample code. Thanks! – Sergio B Jun 04 '14 at 19:23
  • @user2797021 That's not how it works. Try something, test it, debug it, then if you're still having problems, come back, but with specific problems and what you've tried. – David Ehrmann Jun 04 '14 at 21:02
  • Can someone please provide some guidance as to how to accomplish this. I'm new to the java world and I have spent a almost two days on grasping how to accomplish something like this, without itemizing the DOM into a java list, and then sorting it?? but I am not making much progress. Thanks for any help – Sergio B Jun 08 '14 at 00:37