victorio
victorio

Reputation: 6676

What can change the order of elements' attributes in generated XML file by Java?

I use Jaxb2Marshaller and XMLStreamWriter to generate XML files from Java POJO-s from XSD.

And I realized that the order of the attributes in the elements are different, if I run my application in different environments.

I also realized that in my machine it uses the same order as it is in the XSD, and in other machine it looks like it uses descending alphanumeric order.

But I cannot prove this desc. alphanumeric order of course, but it is very likely.

What could cause this order change?

I know that the order of the attributes should not matter in an XML, but really isn't there a solution to set an order for elements' attributes in Java? With any kind of library of course.

Upvotes: 0

Views: 1217

Answers (2)

b0gusb
b0gusb

Reputation: 4731

JAXB uses reflection for marshalling. The list of fields returned through reflection is not sorted in any particular order (see here). Therefore, when marshalling, the order of the attributes does not necessarily reflect the order in the XSD or the Java POJO. Most probably, different Java versions on different operating systems may implement reflection differently so the order may change.

If you only need to be consistent across different installations, sort them alphabetically using @XMLAccessorOrder.

This might help as well.

Upvotes: 0

sebi88
sebi88

Reputation: 158

I would check, if HashMaps or HashSets are involved in processing (and similar collections / classes ... ).

The order of the elements of the iterator of these collections are not defined, can be implemented differently between JVM versions and imlementations

If a database select is involved, the order of the elements can be different (by vendor, version, machine, state), when no explicit 'order by' is used. E.g. on the postgresql, on my PC the last touched elements come first, then the others

Upvotes: 1

Related Questions