This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am using JAXB to unmarshal a xml file to Java object. XML file is :
My requirement is that I need to read a list of "Tenors" in the same order defined in xml file.
I have changed java classes generated by xjc utility to use LinkedHashSet. This is to make sure that I read Tenors in their insertion order. But I am getting elements in order [1Y, 30Y, 5Y, 10Y, 7Y, 2Y]. This means that parser is not reading the list in sequence. If I change the order of elements in XML file still LinkedHashSet contains elements in [1Y, 30Y, 5Y, 10Y, 7Y, 2Y] order only.
Any pointers on how to use JAXB to read elements in order would be much appreciated. If it is not possible using JAXB, any other way would also be fine to me.
Well, if it were me and I wanted things in document order, I would just be using a List instead of messing about with Sets. But like tsuji, I doubt you are getting data in random order too. So perhaps you could explain why you think the data is being stored in that order? Perhaps show us your code which displayed the output you posted?
Joined: Jan 18, 2011
The reason I asked op to at least partially, better fully if not too annoying, prove his case is that I can hook up rapid test cases using no customization and using either xs:sequence, xs:all or xs:choice model group and all results in document order as expected... The List<String> is realized in ArrayList<String> by default for "tenor"... Hence, a proof of the case is desirable.
Joined: Aug 28, 2012
Thanks tsuji and Paul for replies.
I reverted code back to use List instead of Set and I changed default List used (ArrayList) to LinkedList for maintaining the order at retrival time. After doing this I am getting elements in same order as in document.
I still suspect that the problem was in the way you were displaying the LinkedHashSet. But anyway I consider that using List rather than Set in a place where ordering is significant is the correct design, so I would leave it at that.