File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes JAXB - Reading elements in same order as defined in xml. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "JAXB - Reading elements in same order as defined in xml." Watch "JAXB - Reading elements in same order as defined in xml." New topic
Author

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

Vikas Mzn
Greenhorn

Joined: Aug 28, 2012
Posts: 2
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

Joined: Jan 18, 2011
Posts: 511
    
    3
I have serious doubt of this happening. To convince myself, could you show how your schema looks like?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jan 18, 2011
Posts: 511
    
    3
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

Joined: Aug 28, 2012
Posts: 2
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAXB - Reading elements in same order as defined in xml.