Yellow Flash
Yellow Flash

Reputation: 872

AccessOrder for Properties in JAXB

In my class, I have more than 80 attributes.

I have to do it into xml file using JAXB using the same order in class.

so please suggest me a propOrder that create automatically or some other way to give in same order as i given in class.

Note:By default i m getting output in alphabetical order

example:

Java object : order[id = 1, item = 121, qty = 10, city = QWE, ..........., addr = ASD]

excepted result : In xml file
    <order>
      <id>1</id>
      <item no>121</item no>
      <qty>10</qty>
      .
      .
      .
      .
      <addr>ASD</addr>
    </order>

Upvotes: 3

Views: 218

Answers (2)

bdoughan
bdoughan

Reputation: 148987

The order that you specify the fields and properties in your class is not significant. This means that when the JAXB (JSR-222) implementation introspects the class it may not see the fields/properties in the same order that you specified them in. Alphabetical order is the easiest way to offer consistent ordering. If you want to specify an order you need to use propOrder on @XmlType.

Upvotes: 3

AurA
AurA

Reputation: 12363

If you are creating the xml from the Java object then use

@XmlType (propOrder={"id","item",..."addr"})

A similar post talks about is and in more details. JAXB and property ordering

For additional check

If you are converting xml to Java object you should use sequence element if you are validating via xsd.

http://www.w3schools.com/schema/el_sequence.asp

Upvotes: 4

Related Questions