Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAXB - Reading elements in same order as defined in xml.

 
Vikas Mzn
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

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.

Many Thanks

-Vikas
 
g tsuji
Ranch Hand
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have serious doubt of this happening. To convince myself, could you show how your schema looks like?
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
g tsuji
Ranch Hand
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vikas Mzn
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

So using Set was causing the unexpected output.

Many thanks.

-Vikas



 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic