Results 1 to 3 of 3
  1. #1
    rvikasr is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

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

    Hi All,

    I am using JAXB to unmarshal a xml file to Java object. XML file is :

    <Header>
    <NumberOfTenors>6</NumberOfTenors>
    <Tenors>
    <Tenor>1Y</Tenor>
    <Tenor>2Y</Tenor>
    <Tenor>5Y</Tenor>
    <Tenor>7Y</Tenor>
    <Tenor>10Y</Tenor>
    <Tenor>30Y</Tenor>
    </Tenors>
    <Type>ABC</Type>
    </Header>

    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

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: JAXB - Reading elements in same order as defined in xml.

    The JAXB implementations I have used maintain the order.
    It makes no sense otherwise as it would have to read in the entries and then move them around (semmingly randomly).
    Just let xjc determine what to use, which will be a List<>. Since List is defined as ordered there is no point further narrowing it.
    When you have reverted the code to whatever xjc generates then try again.
    Should there still be a problem then post an SSCCE (a short compilable program) that shows the problem, and how you are determining the order in the List.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    rvikasr is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default Re: JAXB - Reading elements in same order as defined in xml.

    Thanks Tolls.

    Its working perfect now.

    I regenerated java classes using xjc and unmarshaled without changing anything. Now I am getting elements in same order as defined in xml.

    Many Thanks

    -Vikas

Similar Threads

  1. Replies: 2
    Last Post: 08-15-2012, 03:38 PM
  2. show elements in correct order
    By Shien in forum New To Java
    Replies: 4
    Last Post: 12-30-2011, 06:20 PM
  3. Replies: 3
    Last Post: 10-20-2011, 12:42 AM
  4. Reading random order
    By khh717 in forum New To Java
    Replies: 6
    Last Post: 05-13-2010, 11:24 PM
  5. Replies: 6
    Last Post: 11-19-2009, 12:39 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •