Reputation: 629
I'm un-marshalling some XML to a string using JAXB. However the order of attributes is not the same as the original XML when running our build scripts via Maven. This is failing some unit-tests which pass perfectly fine in Eclipse.
Any ideas why this would happen? Lots of similar questions here but hard to find a solid answer. :)
Upvotes: 4
Views: 4649
Reputation: 21
Your unit test should using Unmarshaller & Marshaller of the XML so that your test are based on the mapping metadata and not the ordering (or String) of the input XML.
However human readability is often a nice feature and allows you to "diff" output files, using @XmlType(propOrder....) caries over attribute ordering giving you what you are after.
@XmlType(propOrder = {
"elementone",
"elementtwo",
"attributeone",
"attributetwo"
})
Upvotes: 1
Reputation: 629
Little update.
So I figured out things a bit better thanks to your answers, and was able to compare some jaxb objects in my unit test. But it was taking a while.
I found this lib to really help me, and I moved on:
Upvotes: 1
Reputation: 23637
You should re-write your unit tests to follow the rules of well-formed XML if they are testing XML. According to the XML spec, attribute order is not important, and your parser does not need to enforce it. This
<element one="1" two="2" />
is the same thing as
<element two="2" one="1" />
You should also be aware of other differences which are considered equivalent in well-formed XML documents, such as empty elements. According to the XML specification:
<element></element>
is the same thing as
<element/>
Your parser may choose to use one or the other, and that should not fail your tests if they are testing for valid XML.
Upvotes: 3
Reputation: 148987
The order in which attributes appear in XML is not significant while the order in which elements occur is. JAXB like most XML technologies does not guarantee the order in which the attributes will appear. Your unit tests will need to account for this.
Note
When using Unmarshaller
& Marshaller
the output XML is based on the mapping metadata and not the ordering of the input XML. The metadata allows you to specify the ordering of elements but not attributes. You can use JAXB's Binder
to marshal into an existing document (DOM).
Upvotes: 4