0

XJC generates class like this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "el"
})
@XmlRootElement(name = "root")
public class Root {

    @XmlElement(required = true)
    protected String el;
    @XmlAttribute(name = "atr")
    protected String atr;
//...
}

I need "atr" to be first element of "Root".

More specific question is how can I force XJC to put fields marked with @XmlAttribute before fields marked with @XmlElement.

What I've tried: 1. Find XJC annotation to control the order of the element. I didn't find any. 2. Use XJC's plugin to manually change the order. However, implClass.fields() returns UnmodifiableMap

aderesh
  • 927
  • 10
  • 22
  • Why do you need this? – lexicore Dec 02 '16 at 14:18
  • @lexicore, we have xsd-like format which can be converted to xsd. Unlike our format, xsd doesn't preserve the order if there is a mix attributes and elements. For example, because attributes go after xs:sequence, xs:choice, it is not possible to retain order(fields can be generated based on both elements and attributes). What we need is to generate fields in the same order as in our xsd-like format. We are using reflection to display/populate these fields. Overall, I need to control the order of fields in a generated class to have the same order as in our xsd-like format. – aderesh Dec 02 '16 at 16:13
  • In principle, you can achieve this with a XJC plugin. See [this](http://stackoverflow.com/questions/9247730/what-is-the-role-of-classoutline-jclass-cclass-in-codemodel) question, you'll have to work on the model level - that is, `CClassInfo`. – lexicore Dec 02 '16 at 16:40
  • 1
    Still what you write does not make much sense to me. There is actually no "order" of fields in a Java class. You say you're using reflection to access fields. But reflection does not guarantee any specific order of fields, see [this question](http://stackoverflow.com/questions/1097807/java-reflection-is-the-order-of-class-fields-and-methods-standardized). So in principle you can influence the order of fields in a Java file, but this does not guarantee you anything for the runtime. Therefore it is useless. – lexicore Dec 02 '16 at 16:44
  • @lexicore, thank you for the hint. I didn't know that fields may not be in the same order during runtime. If it the case, you are right - it makes no sense. I'll check it. – aderesh Dec 02 '16 at 22:03
  • The usual solution is some kind of `propertiesOrder` annotation. And *that* one you should be able to generate/influence via XJC plugin. – lexicore Dec 03 '16 at 09:47

0 Answers0